Quiz
- Can you please explain the redirect method? It's very clear in your reading, but in the one from the website you liked it says we use a format like flask.redirect(location,statuscode,response) so I am a bit confused about how these parameters would be used. Also, could we talk more about status codes, and what's the purpose of them?
I'm glad my reading was clear. You should then ignore the external reading.
But let's discuss. That external tutorial specifies some extra, optional arguments to the
redirect()function that, frankly, I've never found the need for. The defaults work fine.But lets explain status codes. The response from the server to the browser has one of a dozen or so status codes. See status codes. Most of these are rare. Here are a few of the more common ones:
- 200 success
- 301 redirect permanently; the browser should always use the new URL
- 302 redirect temporarily; the browser should use the new URL for now
- 403 forbidden (permission denied)
- 404 not found
- 418 I'm a teapot. This is based on an ancient April Fools Joke.
- 500 Internal Server Error
The Flask redirect uses 302, and maybe we should use 301 instead:
But I don't feel strongly about this.
- Is redirect how we call one of our separate functions? How is it different from making a function call?
No. The argument to the Flask
redirectfunction is an URL, so we use the Flaskurl_for()function to generate that URL. We are *not* calling any of our functions; we are just using the name of one of our functions. - Can we go over flash() and secret in class, I don't entirely understand how they work together.
Sure. We'll learn more about this next week, but here's a preview:
The
flashfunction stores the messages in something called a cookie, which goes to the browser and the browser returns to us. That's how flashing can survive a redirect, which is a great feature of flashing.But because they go to the browser, they could be tampered with, so Flask digitally signs them in order to detect tampering.
To do the digital signature, it needs a secret key, so we give it one.
- No questions, thank you!
Great!