Introduction to Flask - Adopt a Pet!

I am having trouble completing this challenge, I don’t know the correct syntax for the animals and pet functions. I don’t know if my animals function is correct, please help.

I stopped codding on the 12th step, here is my code:


from helper import pets

app = Flask(__name__)

@app.route('/')

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 = '<h1>List of {pet_type}</h1>'

  for pet_type in pets:

    pet = '<li>{pet_type}</li>'

  return html + pet

@app.route('/animals/<pet_type>/int:<pet_id>')

def pet(pet_id):```
2 Likes

I got stuck on step 12 and can’t go any further. Hopefully, somebody can help us both.

I can get all the step except the last. I can’t figure out how to access the image and paragraph with the backslashes.

from flask import Flask
from helper import pets

app = Flask(__name__)

@app.route('/')
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>{pet_type.title()}<h1>"
  pet = pets[pet_type]
  list = ""
  for i in range(len(pet)):
    list += f"<a href='/animals/{pet_type}/{i}'><li>{pet[i]['name']}</li></a>"
  return html + '<ul>' + list + '</ul>'  

@app.route('/animals/<pet_type>/<int:pet_id>')
def pet(pet_type, pet_id):
  pet = pets[pet_type]
  heading = f'<h1>{pet[pet_id]["name"]}</h1>'
  image = f"<img src='{pet[pet_id]['url']}'>"
  paragraph = f"<p>{pet[pet_id]['description']}</p>"
  return heading + image + paragraph

This is how my working solution looks, hopefully that can help you;

from flask import Flask
from helper import pets

app = Flask(__name__)

@app.route('/')
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):
  pet_list = f'<h1>List of {pet_type}</h1>'
  pet_list += '<ul>'
  pet = pets[pet_type]
  for index, p in enumerate(pet):
    pet_list += f"<li><a href=/animals/{pet_type}/{str(index)}>{p['name']}</a></li>"
  pet_list += '</ul>'
  print(pet_list)
  return pet_list

@app.route('/animals/<pet_type>/<int:pet_id>')
def pet(pet_type, pet_id):
  pet = pets[pet_type][pet_id]
  return f"""
  <h1>{pet['name']}</h1>
  <img src={pet['url']} alt='alt text'>
  <p>{pet['description']}</p>
  <ul>
  <li>{pet['breed']}</li>
  <li>{pet['age']}</li>
  </ul>
  """
2 Likes