[Introduction to Flask] - Adopt a Pet Project- Task 12

Hi dear people,
Can you help me figure how to resolve Task 12 in the “Adopt a Pet” project?

I created a for loop prior to the return statement in the animals() function but didn’t manage to concatenate the

  • pets[name]
  • into the html. In general, I’m not sure I understood the html .

    Can you advise what should I write in the animals() function to resolve task 12? Thx

    Hi @david.bador,

    Link to project: Introduction to Flask: Adopt a Pet

    What is the purpose of this decorator?:

    @app.route('/<html>'))
    

    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.

    1 Like

    Can you please provide solution for this project to go through as its becoming complicated since step 12

    Hello, @sandipswain127835764, and welcome to the Codecademy Forums!

    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.

    See How to ask good questions (and get good answers).

    Thank you but I solved it.

    If anyone finished it, can you please share the full code…?

    Thank You.

    I am also stuck on this step. Any assistance (i.e. what’s the correct code) would be greatly appreciated.

    2 posts were split to a new topic: Introduction to Flask: Adopt a Pet Project - UnboundLocalError

    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