Quiz
- Could you film the demo of it working (as you had mentioned in the reading)?
Okay, I'll do that, but I'm behind in a few things, so it might take me a while. I'll demo today. It's not very interesting.
- Could you explain handlers and/vs. processor variables again?
Sure. It's an outgrowth of what I hoped would be helpful pedagogically, but which has had mixed results. I sometimes have forms (in template files) that look like this:
Then, in
app.py, I would have:The pedagogical idea was to make the connection between the two endpoints more explicit, and to put the relevant code all in
app.py. I think some people found that helpful, and others didn't.In real life, we would almost always hard-code the value of the
ACTIONattribute: - When a link is clicked on a website to a different page, is this first associated with a given html file which then calls an associated function with url_for or does the link fist match to a python function which then renders an html template file?
The latter. Flask routes the request to our Python function, which then renders the response. There might not be an HTML file at all! That's how our first examples were.
The template files like F-strings on steroids.
- If we are only using post forms, is it best practice to still check if the request.method is a get? The examples in today's reading only have post forms, but we still check for get forms.
Great question. If your handler function doesn't support GET, Flask will render an error, something like "no handler for GET of /foo". That's fine.
Alternatively, your handler can support GET and create your own custom response, which might just be a different phrasing of that error message, say using flashing, maybe along with a redirect to a better endpoint.
- Is there a difference in how you should handle / route form submissions when the form is part of the base.html file rather than it's own file? I'm trying to connect the example we saw in flask3 countries lookup to the lookup homework assignment
No, there's really no difference. It doesn't matter where the template for the form is located.
All that matters is that it ends up in the rendered page, with a valid METHOD and ACTION.
- As our project grows, when should we start splitting routes into multiple Python files instead of keeping everything in one app.py?
What a great question! In principle, yes, though in practice most teams do not do this.
However, there are some tricky bits because all the Python files would need to share the same
appglobal variable.One option is to have
app.pycontain:And then the teammates files look like this:
Which is deeply weird, because each module is importing the other. But it does work.
A conceptually easier approach is to put all the
app.route()endpoints into the mainapp.pyfile, but with references toSince the team should agree on the endpoints and keep those stable, this approach has some advantages.
Note, the above code is from memory and is just for answering this question conceptually. I haven't double-checked the syntax and such.