See the following version of the function, which is preceded by a route decorator that is the same as your first one :
@app.route("/animals/<pet_type>")
def animals(pet_type):
html = f"<h1>List of {pet_type}</h1>"
html += "<ul>"
for idx, item in enumerate(pets[pet_type]):
html += "<li>" + f'<a href="/animals/{pet_type}/{idx}">' + item["name"] + "</a></li>"
html += "</ul>"
return html
The following was edited on July 20, 2020 to explain that the above code includes some details that were added later than task 12.:
Some of the features within the code listed above, such as providing links to individual profile pages for each pet, were added after task 12. The links were created during task 16.
The function begins by establishing an <h1> header. Thereafter, it initiates an unordered list with a <ul> tag. Then, inside the loop, it populates the unordered list with list items that contain links. Finally, it places a closing </ul> tag at the end of the list, and returns the resulting markup for the page.
Our policy in the Forums is to provide help to users who ask for it, but to avoid posting the solutions. If you have questions about specific issues concerning the project, you may ask them here. Your requests for help may include some of the code that you have developed. Please make sure that any code that you copy and post is properly formatted.
Hi so I understand most of the project from going through it and checking the forums but can someone explain below why the idx (index) comes before the item (nested dictionary object) and also give a deeper explanation of idx and item in the context of the code below for the animals function. It would also be great if someone could explain what ‘enumerate’ does and as such why it’s useful in this exercise as I’d like some more clarity and I don’t really get the purpose from the hint. Would really appreciate any help.
@app.route("/animals/<pet_type>")
def animals(pet_type):
html = f"<h1>List of {pet_type}</h1>"
html += "<ul>"
for idx, item in enumerate(pets[pet_type]):
html += "<li>" + f'<a href="/animals/{pet_type}/{idx}">' + item["name"] + "</a></li>"
html += "</ul>"
return html
@app.route("/")
def index():
return '''
<h1>Adopt a Pet!</h1>
<p> Browse through the links below to find your furry friend:
<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>"
return html
gets me a “URL not found” for all animals and for the life of me I can’t figure out why. any help is much appreciated
Here’s my solution to step 12 - honestly it is not that hard, it’s just that the way the question is worded is a tad verbose and lengthy, in my personal opinion:
@app.route('/animals/<pet_type>')
def animals(pet_type):
html=f"<h1>List of {pet_type}</h1>"
for pet in pets[pet_type]:
html += f'''
<ul><li>{pet['name']}</li></ul>'''
return html