[Tourist Attractions] Build Python Web Apps with Flask

I have restored my code back to the state that I thought had produced the traceback, but now am unable to reproduce the problem.

¯\_(ツ)_/¯

As per the above, perhaps we could leave things as they are for now, unless someone else demonstrates that they have followed the instructions and then received a traceback upon submitting a new location.

Thanks for the great work. :smile:

2 Likes

Hi. I believe I have received this traceback error on step 21 when submitting a new location as explained above. The error is

UnboundLocalError

UnboundLocalError: local variable 'category' referenced before assignment

I have completed the project twice in case I missed something but I ended with the same result. I also notice that the redirect function does nothing when going to the index ("/") route.

def index():

  ## Redirect to locations route function
  return redirect(url_for("locations", category="recommended"))

When going this route nothing appears on screen and the status wheel just keeps spinning.

1 Like

Hello @mackz88, and welcome to the Codecademy Forums.

Please post a copy of your code and the entire traceback, so that someone can diagnose the problem.

@javaninja55993, are you still involved with refining the content of this project? If so, please let us know whether we can identify the nature of the problem, after the most recent poster has posted relevant code along with a copy of the reported traceback. If responsibility for the content of this project has been transferred to someone else, please let us know how we can direct that person’s attention to the reported problem. Thanks for the good work that you have devoted to this project.

I have tried running the code using a different browser (chrome) and no longer am receiving the traceback error. I believe it had something to do with my ad-blocking extensions as turning them off also remedied the issues.

I am however still having issues with the page redirecting after the form entry and when visiting the “/” route.

Here is the traceback if still relevent - https://i.imgur.com/ueUoON9.png

Here is my code for this project - https://gist.github.com/Mmackz/2485cd3913e6550b259fd1b424de3f0d

Yeah, I redid the entire project, checking and rechecking the instructions carefully, and entered a new location:

The result was a traceback:

* #### File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line  *2463* , in  `__call__`

return self.wsgi_app(environ, start_response)

* #### File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line  *2449* , in  `wsgi_app`

response = self.handle_exception(e)

* #### File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line  *1866* , in  `handle_exception`

reraise(exc_type, exc_value, tb)

* #### File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line  *39* , in  `reraise`

raise value

* #### File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line  *2446* , in  `wsgi_app`

response = self.full_dispatch_request()

* #### File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line  *1951* , in  `full_dispatch_request`

rv = self.handle_user_exception(e)

* #### File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line  *1820* , in  `handle_user_exception`

reraise(exc_type, exc_value, tb)

* #### File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line  *39* , in  `reraise`

raise value

* #### File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line  *1949* , in  `full_dispatch_request`

rv = self.dispatch_request()

* #### File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line  *1935* , in  `dispatch_request`

return self.view_functions[rule.endpoint](**req.view_args)

* #### File "/home/ccuser/workspace/tourist-attractions-app-project/app.py", line  *45* , in  `add_location`

return redirect(url_for("locations", category=category))

> UnboundLocalError: local variable 'category' referenced before assignment

It appears that this block in the add_location function did not execute:

  if add_form.validate_on_submit():
      # 20)
      name = add_form.name.data
      description = add_form.description.data
      category = add_form.category.data
      visit.add(name, description, category)

I suspect we need a fix from the Codecademy staff.

2 Likes

Thank you @mackz88 and @appylpye for providing your code and errors here, this is extremely helpful. I will use this to revisit the project today and determine if the issue is with the project code itself or some engineering issue we need to address. I will follow up here.

2 Likes

Thanks, @javaninja55993. Let me know if you need a copy of specific portions my code, where I have included numbers in the comments that correspond to instruction numbers.

I have completed the full project and all runs fine until I try to Add a Location. I get the following error:

ValueError

ValueError: too many values to unpack (expected 1)

Looking further into the error message I see the following:

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

[(name, action)] = request.form.items()

ValueError: too many values to unpack (expected 1)

So it seems like this line that assigns the key, value tuple to the list is not working. I wish there was a way to get to a terminal so that I could see exactly what request.form.items() returns.

Here is my code https://gist.github.com/edac68ec77325b07bc44a03133edadc9

Any help would be appreciated. Thanks!

Your code for step 14 is not sufficient.

There are two <form>s in locations.html file. One is for “up” and “del” button without action assigned. And the other one is for “Add Location” button. To process this request you need to assign add_location() function to action attribute.

Line 44 in locations.html
<form class="addform" action="{{ url_for('add_location') }}" method="POST">

That did the trick. After a couple of other errors I found once this was corrected, I can now add locations.

One last question, when I move up or delete a location, the screen refreshes but I still see the location on the page. It is only after I refresh the page for the category that the screen is updated. This also introduces a bug if I try to delete the same item twice. Getting a NoneType error since the location is no longer in the list.

I am guessing it has to do with the way I am returning the page in the locations route. Line 26 in app.py

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

Thanks for the quick response. Btw, the Flask skill set is a great new addition to Codecademy!

1 Like

That happens because locations variable in locations() is set before process your “up” or “delete” action. You need to set locations variable right before returning to web browser. And as you said. this might also be a bug…

def locations(category):
  # Set locations variable later
  # 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)

  locations = visit.get_list_by_category(category)
  ## Return the main template with variables
  return render_template("locations.html", category=category, categories=categories, locations=locations, add_location = AddLocationForm())

FYI, there is a small typo in the second screen of DATABASES IN FLASK - READING, UPDATING AND DELETING / Queries: query.all() and query.get()

Task 3:
In the ‘playground.py’ file, write code that to a variable called book_1 assigns a book with id = 12 using the get() function.

The note in the code has the id = 13. If you use id = 12 as the task instructs, you will get an error.

Yes @mikeiap I also saw this. This is a bug also.

I have the same problem may I know what’s a solution for this matter ?
Thanks

Hello @holyking, and welcome to the Codecademy Community!

As engineering issues are resolved concerning this project, @javaninja55993 plans to follow up in this thread.

See this post.

Hello @javaninja55993,

Still running into these issues. Is there a planned update for this project?

1 Like

jinja2.exceptions.UndefinedError: ‘forms.AddLocationForm object’ has no attribute 'hidden_tag’

Did someone had this error.

app.py

@app.route("/add_location", methods=["POST"])
def add_location():
  ## Validate and collect the form data
  add_form=AddLocationForm()
  if add_form.validate_on_submit():
      name=add_form.name.data
      description=add_form.description.data
      category=add_form.category.data
      visit.add(name, description, category)

  ## Redirect to locations route function
  return redirect(url_for("locations"), category=category)

locations.hml

  <form class="addform" action="{{ url_for('add_location') }}" method="POST">
    {{ add_location.hidden_tag() }}

So on December 24-th there is still that problem with
“UnboundLocalError: local variable ‘category’ referenced before assignment”
message.
I suppose it’s not fixed yet.
But without that, also thank you for trying to set it the correct way!
@appylpye, @javaninja55993

I am having the same issue now. Did you ever figure it out?

Edit: I finally figured out the issue. For me I had not put AddLocationForm as a class and did not have it inheariting from FieldRequiredForm. Here is my fixed code which seem to work now:

class AddLocationForm(FieldsRequiredForm):
  name = StringField("Location Name", validators=[DataRequired()])

  description = TextAreaField("Location Description", validators=[DataRequired()])

  category = RadioField("Category", choices=[categories])

  submit = SubmitField("Add Location")