I am on the project adopt a pet and all seems to be going well until step 14 where my output from the pet variable is nothing instead of the the expected correct output which should be of the form:
I have spent hours trying to figure out the problem to no avail. Can anyone help me have a look and hint me where the problem might be? I have attached the relevant code below:
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>
"""
At this point, output is:
# Adopt a pet
Browse through the links below to find your new furry friend:
* Dogs
* Cats
* Rabbits
Then the step:
@app.route("/animals/<pet_type>")
def animals(pet_type):
html = "<ul>" + f"<h1>List of {pet_type}</h1>"
for i, item in enumerate(pets[pet_type]):
html += f"<li><a href='/animals/<pet_type>/<int:pet_id>'>{[item['name'], i]}</a></li>"
html += "</ul>"
return html
Here output for the Dog link for example is:
# List of dogs
* ['Spot', 0]
* ['Shadow', 1]
Then below, I have no output at all. Can anyone please hint me?
@app.route("/animals/<pet_type>/<int:pet_id>")
def pet(pet_type, pet_id):
pet = pets[pet_type][pet_id]
return f"<h1>{pet}</h1>"
Ok, so first thing you might want to do is verify that this line is working for you:
pet = pets[pet_type][pet_id] either in flask shell or via print statement to the terminal.
Then, you can check the link works and that the information is being fed to it properly. Note you need both the type and id parameters for the link to work.
Example:
print(pets[cats][2]) # if this works
www.petshop.com/animals/cats/2
# then this should have to work
# note i put a fake domain, it should be whatever
# domain or localhost is you're testing on
Thank you so much for putting this up!! I was so completely stumped and literally gave up. I couldn’t get past step 14 and there was no help or documentation ANYWHERE! On top of that the coder community out there was absolutely no help either! They kept saying they couldn’t just put up the answers. That is not really true since there have been all sorts of answers to problems in EVERY course I have taken with CodeCademy so far! Your answer really helped me finish that project. Your snippet of code isn’t what I ultimately used, but it showed me the right path to go on to come up with a solution that worked for me! All those pro coders out there with their snooty attitudes and I got the help I needed from a newbie like myself! Thank you!
I’m actually struggling with step 14 as well and specifically getting this line of code to work:
pet = pets[pet_type][pet_id]
It appears that in the it doesn’t recognize my <pet_type> variable declaration within the @app.route decorator and only interprets it as a string. The code I used for this is as follows:
@app.route(’/animals/<pet_type>/<int: pet_id>’)
The error I received is “ValueError: malformed url rule:” so I can only assume that there is first something wrong with the url within the decorator.