Home > Mobile >  Could not build url for endpoint 'otp' with values ['adhar_no']. Did you mean &#
Could not build url for endpoint 'otp' with values ['adhar_no']. Did you mean &#

Time:05-27

can anyone help me with this code from flask I am getting this error Could not build url for endpoint 'otp' with values ['adhar_no']. Did you mean 'vote' instead?

from flask import Flask, request, render_template,url_for,redirect





parties=["A","B","C","D"]


app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template('home.html')
    #return render_template('admin.html')

@app.route('/', methods=['POST'])
def my_form_post():
    adhar_no = request.form['aadhar_num']

    return redirect(url_for('otp',adhar_no=adhar_no))
    return render_template('otp.html',adhar_no=adhar_no)

@app.route('/otp',methods = ['POST'])
def verification():
    a_no=request.args.get('adhar_no')
    otp_text = request.form['otp']
    return a_no

HTML CODE
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>VERIFY</title>
    <link rel="stylesheet" href="/static/css/style.css">

</head>

<body>
    <div >
        <h1>Verification</h1>
        <form action="{{ url_for("verification")}}" method="POST">
            <div >
                <input type="text" name="otp" required pattern="[0-9]{4}" maxlength=4 id="userCode" ng-model="formdata.userCode" title="4 Digit code">&nbsp;
                <label>Enter otp </label>
            </div>
            <strong>{{error}}</strong> 
            <button >VERIFY</button>
        </form>
    </div>

    <script src="/static/js/sjs.js"></script>
</body>

</html>


and I am getting this error

werkzeug.routing.BuildError: Could not build url for endpoint 'otp' with values ['adhar_no']. Did you mean 'vote' instead?

CodePudding user response:

Flask url_for will require a function name. You were passing the otp and there is no otp function hence the error

return redirect(url_for('.verification',adhar_no=adhar_no))

For verification function in /otp route you will also need to change the function signature.

Refer to the url_for documentation for more info

CodePudding user response:

Note: url_for function take input of your existing route function.

return redirect(url_for('otp',adhar_no=adhar_no))

here you passed otp in url_for as a function name(which does not exist in your existing routes),

whereas you must pass verification in url_for.

E.g: return redirect(url_for('verification',adhar_no=adhar_no))

Anyhow, Your verification function accepting only POST request, and you are trying to send query param adhar_no with GET request.

  • Related