I’m currently working on the project from the Jinja2 Templates and Forms section of this course (https://www.codecademy.com/learn/paths/build-python-web-apps-flask).
I’ve followed the instructions to the letter, and everything worked well until the last two steps involving collecting the form data and redirecting the application flow.
28 @app.route("/add_location", methods=["POST"]) 29 def add_location(): 30 ## Validate and collect the form data 31 add_form = AddLocationForm() 32 33 if add_form.validate_on_submit(): 34 name=add_form.name.data 35 description=add_form.description.data 36 category=add_form.category.data 37 visit.add(name, description, category) 38 39 ## Redirect to locations route function 40 return redirect(url_for("locations", category=category)) 41 42 @app.route("/") 43 def index(): 44 45 ## Redirect to locations route function 46 return redirect(url_for("locations", category="recommended"))
When I try to submit a new location in the browser, I get an “UnboundLocalError: local variable ‘category’ referenced before assignment” which points to File “/home/ccuser/workspace/tourist-attractions-app-project/app.py”, line 40, in
It seems to me that once I fill out the form and click submit,
add_form.validate_on_submit() should evaluate to
True, which would create the variable
category and cause it to be assigned the value that exists in the
category field of the
add_form object. If this were the case though, wouldn’t category be assigned before I reference it in line 40?
Thanks in advance for any feedback.