Protocol Protocol - 4 months ago 28
Python Question

Flask HTML forms- UnboundLocalError

I am new to the Flask application development. I want to make a Flask application that receives the details from a user. After the successful registration, then a new page will be displayed showing the entered details. The application is built using HTML forms and not by using WTF.

Following is the code for app.py

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/register',methods = ['POST', 'GET'])
def user():
if request.method == 'POST':
result = request.form
name = result['name']
phoneno = result['phoneno']
email = result['email']
password = result['password']
if name or phoneno or email or password is not None:
print name,phoneno,email,password
return render_template("register.html",result=result)

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


This is the HTML page of register.html

<html>
<body>

<form action = "/success" method = "POST">
<p>Name <input type = "text" name = "name" /></p>
<p>Phone no <input type = "text" name = "phoneno" /></p>
<p>Email <input type = "text" name = "email" /></p>
<p>Password <input type ="text" name = "password" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>

</body>
</html>


What now I am getting is an error

Full Traceback
**UnboundLocalError**
UnboundLocalError: local variable 'result' referenced before assignment.

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/protocol/PycharmProjects/tut/app.py", line 17, in user
return render_template("register.html",result=result)
UnboundLocalError: local variable 'result' referenced before assignment


I tried to print the values. But nothing is shown in the console I couldn't rectify this error.Thanks in advance.

Answer

result only exists if the request method is POST, so if the request method was GET(which is the default request one), that is when the page is requested before submitting user data form, then result won't be existing, hence the error message. Plus, you need to render the result object into another template, for example user.html:

@app.route('/register',methods = ['POST', 'GET'])
def user():
    if request.method == 'POST':
        result = request.form
        name =  result['name']
        phoneno = result['phoneno']
        email = result['email']
        password = result['password']
        if name or phoneno or email or password is not None:
            print name,phoneno,email,password
            return render_template("user.html",result=result)
    return render_template("register.html")

Another thing, if the above view function is for register.html form, then you also need to change in form block, from:

<form action = "/success" method = "POST">

to

<form action = "/register" method = "POST">
Comments