[BUILD PYTHON WEB APPS WITH FLASK] Tourist Attractions Step 10

I’m having trouble with step 10 of this exercise, it tells us:

In app.py collect the name and value data from the submitted form. Replace the [(None, None)] statement with the output of the items() method of the request objects form attribute.

When you are done you can test out the functionality by clicking the buttons next to each location. If you delete all the data you can refresh it by running the code again.

In my code, I built it out properly.

@app.route("/<category>", methods=["GET", "POST"]) def locations(category): locations = visit.get_list_by_category(category) if request.method == "POST": [(name, action)] = request.form.items() if action == UP_ACTION: visit.moveup(name) elif action == DEL_ACTION: visit.delete(name)

But still unsure why it;s not working. Everything else in my code is working as expected, and I have finished the project. I just cannot move it up or delete any lists without this error being thrown back:

ValueError

ValueError: not enough values to unpack (expected 1, got 0)

unpacking values happens when you have something like

a, b, c = collection_with_three_items

this would work well if you had

x = [6, 3, 15]
a, b, c = x
# now c = 15, a = 6 ... etc

Now expecting 1 and getting 0 is a bit rare. Look at the line number where the error says this happens.
If you put a trace statement (import trace; pdb.set_trace()) you can query what the value you’re trying to evaluate there actually is.

If we try to unpack this way

x = [4]
[a] = x
# a takes on the value of 4

we unpack the first value of x onto a.

So [(name, action)] = request.form.items()
[(name, action)] means that it’s expecting a collection holding single item that has two items inside like { (3, 4) } or [ [1,2] ]
but instead it’s getting an empty collectiion {}.

1 Like

At Step 10 - I got TypeError, and cannot go forward.

TypeError when click moveUp:

TypeError: list indices must be integers or slices, not NoneType

TypeError when click delete:

TypeError: ‘NoneType’ object cannot be interpreted as an integer

I think so far I have built my code properly.

app.py :point_down:

from flask import Flask, render_template, request, redirect, url_for
from locations import Locations

app = Flask(__name__)
app.config['SECRET_KEY'] = 'SECRET_PROJECT'

visit = Locations()
categories = {"recommended": "Recommended", "tovisit": "Places To Go", "visited": "Visited!!!", }

UP_ACTION = "\u2197"
DEL_ACTION = "X"

@app.route("/<category>", 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, categories=categories, locations=locations)

@app.route("/add_location", methods=["POST"])
def add_location():
  ## Validate and collect the form data

  if True:
      name=None
      description=None
      category=None
      visit.add(name, description, category)

  ## Redirect to locations route function
  return ""

@app.route("/")
def index():

  ## Redirect to locations route function
  return ""

Help! :smiling_face_with_tear: