Hi all! I’m working on the Adopt A Pet project as seen here in the Intro to Flask module. I’m running into a bit of a frustrating issue. I have nearly all of the setup working, except when I click into the hyperlinks created for the names of the pets (leading to a URL such as “https://localhost/animals/dogs/0”), I get a Not Found error.
I’ve found this module to be absolutely riddled with bugs and hit so many errors that are resolved simply by hitting Run a bunch of times, but this doesn’t seem to be one of those as I still hit the error when I run the script on my local Python3 instance. ChatGPT insists that my logic is sound and I don’t see what’s wrong, so I could really use an additional pair of eyes. The error in logic is likely going to be within the “Pet” view function.
Could someone smarter than myself take a look at my logic and let me know what I’m missing?
from flask import Flask
from helper import pets
app = Flask(__name__)
@app.route('/')
def index():
return f'''
<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>
'''
@app.route('/animals/<pet_type>')
def animals(pet_type):
html = f'<h1>List of {pet_type}</h1><br><ul>'
for pet_id,pet in enumerate(pets[pet_type]):
html += f'<li><a href="/animals/{pet_type}/{pet_id}">{pet["name"]}</a></li><br>'
html +='</ul>'
return f'''
{html}
<a href="/">Return to home</a>
'''
@app.route('/<pet_type>/<int:pet_id>')
def pet(pet_type,pet_id):
pet = pets[pet_type][pet_id]
return f'<h1>{pet["name"]}</h1>'
Set this up locally. If you do, you can place a statement like import pdb; pdb.set_trace()
to set a break point at the exact place right before you link call is called.
Then from there you could query the status of your variables to make sure they are what you want them to be and there’s no mismatched.
For example if you have a simple adding function
def add(x, y):
import pdb; pdb.set_trace()
return x + y
add(4,5)
add(7, "cat")
The first time your breakpoint gets hit, the program will give you an interactive prompt (this would be on your flask server side window)
and you can type there
>>> x
4
>>> y
5
>>> c # this makes the program continue until next breakpoint
>>> # now it should hit the second add() call
>>> x
7
>>> y # here we're going to have the problem
"cat"
Side note, it might be very annoying that the module has these bugs, but learning debugging at an early stage is incredibly useful. I’ve met people that were not aware of the breakpoints you can set in flask. It’s a huge timesaver, especially when you have deadlines. Some might say, isn’t set_trace() outdated? Yes, now it’s breakpoint(), but you might be on a python <= 3.6 project. The point is you’re aware that this is possible and easy to do.
On a second note, I notice you’re trying to get gpt to debug. Not a bad idea. If you also give it the extra information from your own debugging with breakpoints it can be even more accurate.
1 Like