A web page often has "supporting" files, like CSS (often several of those), JS (several of those) and images (logos, branding, etc). (Do a "view source" on this page.)
All
of those are static, and can be simply put in a static
folder and sent to the browser when it requests them.
Flask takes care of all of that for us.
Sure. The template file is an HTML page, but with places in it for filling in data, marked with double braces, like:
You were right the first time. A form is written using the HTML language, which we typically put in template files. It might have some dynamic parts, but it's mmostly static. We'll see some examples today.
Glad to. An HTML template file might look like:
<!doctype html>
<head>
<title>Homepage</title>
<link type="text/css"
rel="stylesheet"
href="{{ url_for('static', filename = 'style.css') }}">
<script src="{{ url_for('static', filename = 'frontend.js') }}">
</head>
<header><img src="{{ url_for('static', filename = 'homepage-logo.png') }}" alt="logo"></header>
<h1>This is the home page for {{username}}</h1>
Yes. I can imagine small snippets, but mostly our responses will
be complete pages, with CSS and JS and images and stuff like that, so
too bulky and static to clutter up our app.py
with.
We use GET
when the purpose the request (the URL or
the form submission) is to GET some information from the server.
We use POST
when the purpose of the request (the form
submission) is to POST (upload, update, insert, delete) some
information to the server.
A GET request (URL) can be meaningfully saved, bookmarked, sent to a friend, etc. The page that shows a book on Amazon.com.
A POST is a one-time thing. (Typically) The page that orders a book on Amazon.com
It puts them in different dictionary-like objects:
request.args
for GET
request.form
for POST
They are identical as data structures; you use them the same way.
I usually use the .get()
method, since that won't raise an exception if the data is missing.
I haven't found any, but I haven't tried to do anything super fancy (e.g. recursion over a tree). It's intended for rendering data structures, so iteration and conditionals is about as fancy as I've ever needed.
We'll do some now!