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
Hi.
It showed me syntax errors, but I took 1 hour break and now i solved that problem
But now I have one small problem. I think problem is in this tag:
I’ve got it! I found how to solve that problem
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
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.