Templates and Forums - Tourist Attractions

I don´t understand what do I have to do on step 2, it says:

Good job. Our web content doesn’t look like much, just a couple buttons that are part of a web form. In order to work on our template we’ll need to create some template variables and assign the application data to them. Currently our application data consists of:

  • category : The URL string variable that defines the current category page.
  • categories : A dictionary with the category string as the keys and display ready categories as the items.
  • locations : A list of Location() objects as defined in locations.py . This is a list of only the locations associated with the string variable category .

In app.py add 3 variables using keyword arguments:

  1. Create template variable category and assign it the app variable category
  2. Create template variable categories and assign it the app variable categories
  3. Create template variable locations and assign it the app variable locations

And this is the code:

from locations import Locations

app = Flask(__name__)

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

UP_ACTION = "\u2197"

@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 False:
    [(name, action)] = [(None, None)]

    if action == UP_ACTION:
    elif action == DEL_ACTION:
  ## Return the main template with variables
  return render_template("locations.html")

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

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

  ## Redirect to locations route function
  return ""

def index():

  ## Redirect to locations route function
  return ""```
1 Like

So when you return render_template(), that’s your chance to set the variables that are going to be rendered in the loaded template.

It might look something like

return render_template("something.html",\\
   category=category, categories=categories,\\

It basically takes the variable that’s affected and passes it on to the next rendering.

Let me know if that makes sense.

1 Like

hey thanks for the help, but where do I put this code?

This seems like it goes in your add_location() function. But it should make sense (you need to have declared these variables first for it to work).