[Tourist Attractions] Build Python Web Apps with Flask Get Help With Error Code

Currently working on the https://www.codecademy.com/paths/build-python-web-apps-flask/tracks/introduction-to-flask/modules/flask-templates-and-forms/projects/tourist-attractions-app project:

I am finished, but I keep getting the same error code when trying to run it…

NameError: name ‘categories’ is not defined

Traceback (most recent call last)

  • File “/home/ccuser/workspace/tourist-attractions-app-project/app.py”, line 25 , in locations

return render_template(“locations.html”, category = category, locations = locations, categories = categories, add_location = AddLocationForm())

NameError: name ‘categories’ is not defined

This is my current code for context:

@app.route("/", methods=[“GET”, “POST”])
def locations(category):
locations = visit.get_list_by_category(category)

Check the request for form data and process

if request.method == “POST”:
[(name, action)] = request.form.items()

if action == UP_ACTION:
  visit.moveup(name)
elif action == DEL_ACTION:
  visit.delete(name)

Return the main template with variables

return render_template(“locations.html”, category = category, locations = locations, categories = categories, add_location = AddLocationForm())

NameError: name ‘categories’ is not defined

It has to be defined in your function before you can pass it onto render_template as categories=categories.

If it’s only under conditionals, that means that in some conditions it won’t be defined, so when the render_template is called, flask won’t know what to set categories to.

Some possible solutions are setting it to a default value in the function. Another is to serve the information in a route that just returns a JSON object.

I know. it is defined in my forms.py document and is imported into app.py. here is the forms.py code to show you where it is referenced

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField, RadioField
from wtforms.validators import DataRequired

categories = [(“recommended”,“Recommended”), (“tovisit”, “Places To Go”), (“visited”, “Visited!!!”)]

Create Form Here

class AddLocationForm(FlaskForm):
name = StringField(“Location Name”, validators = [DataRequired()])
description = TextAreaField(“Location Description”, validators = [DataRequired()])
category = RadioField(“Categories”, choices = categories)
submit = SubmitField(“Add Location”)

You could do a simple test whether the import is working.

Remove categories=categories temporarily. Then, in the context of the function do a print(categories) to see if it pops up.

If it’s not popping up something is up with your import of forms.py possibly.

didn’t work. I’m just going to leave it. too much annoyance right now for me to go through all of the code on all of the forms and figure it all out. I have had enough trouble with it that I’m just moving on. Thanks though.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.