Problem with Flask project "Adopt a Pet"

Hello. I started new path “Build Python web apps with Flask” and I stuck in first project…

https://www.codecademy.com/paths/build-python-web-apps-flask/tracks/introduction-to-flask/modules/introduction-to-flask/projects/adopt-a-pet

I think until 13th task my code works properly, but when I try to do next tasks I get only errors.

from flask import Flask
from helper import pets

app = Flask(__name__)

@app.route('/index')
def index():
  return '''
  <h1>Adopt a Pet!</h1>
  <p>Browse through the links below to find your new furry friend:</p>
  <ul>
    <li><a href='/animals/dogs'>Dogs</a></li>
    <li><a href='/animals/cats'>Cats</a></li>
    <li><a href='/animals/rabbits'>Rabbits</a></li>
  </ul>
  '''

@app.route('/animals/<pet_type>')
def animals(pet_type):
  html = f'<h1>List of {pet_type}</h1>'
  html += '<ul>'
  for x in pets[pet_type]:    
    name = x["name"]
    html += f'<li>{name}</li>'
    
  html += '</ul>'
  return html

@app.route('/animals/<pet_type>/<int:pet_id>')
def pet(pet_type, pet_id):
  pet = pets[pet_type[pet_id]]

  return pet[name]

Can someone explain me how to finish this project?
I tried to modify for loop in “animals” function according to task 16th, but then I get errors.

def animals(pet_type):
  html = f'<h1>List of {pet_type}</h1>'
  html += '<ul>'
  for num, x in enumerate(pets[pet_type]):    
    name = x["name"]
    html += f'<li><a href='/animals/pet_type/num'>{name}</a></li>'
    
  html += '</ul>'
  return html

Regards

Hi,

I’d like to help!

What are the errors specifically?

Welcome to the forums :slight_smile:

Hi.
It showed me syntax errors, but I took 1 hour break and now i solved that problem :slight_smile:
But now I have one small problem. I think problem is in this tag:

 for pet_id, x in enumerate(pets[pet_type]):    
    name = x["name"]
    html += f'<li><a href="/animals/pet_type/pet_id"> {name} </a></li>'

this tag treats pet_type and pet_id as strings, but it should treats them as variable. How to solve that problem?
so, for example when I click in that link it should sends me to localhost/animals/dogs/1
( but it sends me to localhost/animals/pet_type/pet_id)

I’ve got it! I found how to solve that problem :slight_smile:
I’m attaching below my solution for this project.
Regards

from flask import Flask
from helper import pets

app = Flask(__name__)

@app.route('/index')
def index():
  return '''
  <h1>Adopt a Pet!</h1>
  <p>Browse through the links below to find your new furry friend:</p>
  <ul>
    <li><a href='/animals/dogs'>Dogs</a></li>
    <li><a href='/animals/cats'>Cats</a></li>
    <li><a href='/animals/rabbits'>Rabbits</a></li>
  </ul>
  '''

@app.route('/animals/<pet_type>')
def animals(pet_type):
  html = f'<h1>List of {pet_type}</h1>'
  html += '<ul>'
  for pet_id, x in enumerate(pets[pet_type]):    
    name = x["name"]
    html += f'<li><a href="/animals/{pet_type}/{pet_id}"> {name} </a></li>'
    
  html += '</ul>'
  return html

@app.route('/animals/<pet_type>/<int:pet_id>')
def pet(pet_type, pet_id):
  pet_list = pets[pet_type]
  pet = pet_list[pet_id]
  html = f'<h1>{pet["name"]}</h1>'
  html += f'<img src={pet["url"]}>'
  html += f'''
  <ul>
    <li>breed: {pet["breed"]}</li>
    <li>age: {pet["age"]}</li>
  </ul>'''

  return html

So the problem wasn’t in the quoted code then? Where was it, if you don’t mind sharing?

It was in quoted code which I shared in first post.
I didn’t know how to create ‘for loop’ for this exercise.
Finally I found solution and I posted correct version of this code.