Flask Toolkit
Contents
Basic application code
| |
Routing
| |
Browsers automatically append a trailingslash (canonical URL).
If the route does not end with a trailing slash, a client accessing a canonical URL will receive a 404
POST requests
| |
#Variable Rules
| |
@app.route(‘/path/
1 2 3 4 5 6 7 8 9 10 11 | |Converter Type|Description|
|:--|:--|
string|(default) accepts any text without a slash
int|accepts positive integers
float|accepts positive floating point values
path|like string but also accepts slashes
uuid|accepts UUID strings
# Static Files
```python3
url_for('static', filename=<FILENAME>) |
Store files in the static directory
Template Rendering
Templating uses Jinja2 as the processor
Store templates in the templates directory
Accessing request data
| |
Cookies
Set cookies -> <response>.set_cookie(<KEY>, <VALUE>)
Get cookies -> request.cookies.get(<KEY>)
Redirect
flask.redirect(<PATH>)
Errors
flask.abort(<STATUS_CODE>)
Error Handlers
| |
Session Management
``flask.session
Session is stored INSIDE the client’s cookies (encrypted with the secret key)
$ python -c ‘import os; print(os.urandom(16))’ b’_5#y2L”F4Q8z\n\xec]/’
Message flashing
Putting data created from one response into another request (ie to write ‘successful login’)
Deferred Request Callbacks
We can write functions that will execute before (and) after a was reached
Apply the @<app>.before_request decorator onto the function
Apply the flask.after_this_request decorator onto a function that is defined inside