Hello,

i have a sample web appliaction which get a value from HTML text feild and redirect to HTML page according to the value passed
but the problem is that i can't get the value entered in the html text throught (request.form) method

I get the below error
werkzeug.routing.BuildError: Could not build url for endpoint 'suc'. Did you forget to specify values ['name']?

from flask import Flask ,redirect, url_for, request

app = Flask (__name__)

@app.route('/suc/<name>')
def suc(name):
    return "hello Boss %s" % name

@app.route('/login', methods = ['GET','POST'])
def login():
    if request.method == 'post':
        user = request.form['nm']
        return redirect(url_for('suc',name = user))
    else:
        user = request.args.get('nm')
        return redirect(url_for('suc',name = user))

if __name__ == '__main__' :
    app.run(debug = True)

<html>
   <body>

      <form action = "http://localhost:5000/login" method = "post">
         <p>Enter Name:</p>
         <p><input type = "text" name = "nm" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>

   </body>
</html>

Im sure that the value of text not passed correctly to user varable becouse when i change the code and pass the value name like the following :-
return redirect(url_for('suc',name = 'hossam'))`
The code works fine

To set it up more,correct and fix error.
Capitale letters in method == 'POST'
Have remove GET to avoid to confusion.

foo\
app.py
    templates\
    index.htm

app.py

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

app = Flask (__name__)
@app.route('/')
def my_form():
    return render_template("index.html")

@app.route('/suc/<name>')
def suc(name):
    return "hello Boss %s" % name

@app.route('/login', methods=['POST'])
def log():
    if request.method == 'POST':
        user = request.form['nm']
        return redirect(url_for('suc', name=user))

if __name__ == '__main__' :
    app.run(debug=True)

index.html

<html>
   <body>
      <form action = "http://localhost:5000/login" method="post">
         <p>Enter Name:</p>
         <p><input type="text" name="nm" /></p>
         <p><input type="submit" value="submit" /></p>
      </form>
   </body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.