[Tourist Attractions] Build Python Web Apps with Flask

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")

Hi,

Declare the category variable before the if statement. It should be empty or the chosen category.

add_location()
def add_location():

  ## Validate and collect the form data

  add_form = AddLocationForm()

  category = add_form.category.data

  if add_form.validate_on_submit():

      name = add_form.name.data

      description = add_form.description.data

      visit.add(name, description, category)

  ## Redirect to locations route function

  return redirect(url_for(

    "locations",

    _external = True,

    category = category,

    _scheme = "https"

  ))