Sure, I'd be glad to.
Here's one longish sentence:
A browser requests a page from the server, and the server responds to that request either with a fixed, unchanging (static) page such as an "about" page, or with a dynamically computed page, such as the latest updates on a social media site.
Templates are the static parts of a dynamic page. If I have some python code like this:
Thetmpl
is actually a constant, static part of
the printed result, while the num1
is dynamic.
Now, imagine the tmpl
being a 200 line HTML
file. You'd much rather put that in a file. So it becomes a template file.
The template provides the static parts of the response.
the tmpl
above is a template with two variables
(fillable areas). In Jinja2, we create files with as many fillable
areas as we want.
the render_template
is not creating the file. It is
filling in the fillable areas and creating a page to send to the
browser.
The template file is always static. If there are no dynamic parts, then the page is static.
A web page also has supporting files, like CSS files, JS files, images like logos and background images. Those are all static.
It points to a function! The function that generates the response for that URL.
In bigger, fancier Flask applications, they can be divided up. In
our course, at least for now, they will all be in app.py
.
I mean don't do redirect('/nm'+val)
.
Do use either render_template
OR redirect(url_for(function_name))
.
The word "get" is being overworked. GET is a format for a request. POST is the other. They are mutually exclusive. That means that a request is either sent using GET or POST. Never both. Postcards versus Envelopes.
The flask app gets the request.
request.args
(the front of the postcard)
request.form
(the inside of the envelope)
Both of those things are dictionaries.
To extract information from a dictionary, you can
use dict[key]
or dict.get(key)
. The latter
is an entirely different get
.
Caching is when something that has been computed or retrieved is saved in case it's needed again. With POST, since it represents an update, we don't want the prior data, we want new data.
An alternative for {% %}. We won't use them, so ignore this.