Flask: Difference between revisions
Jump to navigation
Jump to search
Line 93: | Line 93: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Template=== | ===Template=== | ||
Pretty basic stuff | |||
<syntaxhighlight lang="css+jinja"> | |||
{% extends "base.html" %} | |||
{% block content %} | |||
<h1>Sign In</h1> | |||
<form action="" method="post" novalidate> | |||
{{ form.hidden_tag() }} | |||
<p> | |||
{{ form.username.label }}<br> | |||
{{ form.username(size=32) }} | |||
</p> | |||
<p> | |||
{{ form.password.label }}<br> | |||
{{ form.password(size=32) }} | |||
</p> | |||
<p>{{ form.remember_me() }} {{ form.remember_me.label }}</p> | |||
<p>{{ form.submit() }}</p> | |||
</form> | |||
{% endblock %} | |||
</syntaxhighlight> | |||
===Rendering=== | |||
<syntaxhighlight lang="py"> | <syntaxhighlight lang="py"> | ||
from flask import render_template | |||
from app import app | |||
from app.forms import LoginForm | |||
# ... | |||
@app.route('/login') | |||
def login(): | |||
form = LoginForm() | |||
return render_template('login.html', title='Sign In', form=form) | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 05:55, 23 May 2021
Introduction
Quick tour of the python framework flask. Most of this has been taken from https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
Getting Started
from app import app
@app.route('/')
@app.route('/index')
def index():
user = {'username': 'Miguel'}
return '''
<html>
<head>
<title>Home Page - Microblog</title>
</head>
<body>
<h1>Hello, ''' + user['username'] + '''!</h1>
</body>
</html>'''
Routing
Templates
So flask like others such pug or egs has templates. Very similar indeed. It supports inheritance for navigation and footers
from flask import render_template
from app import app
@app.route('/')
@app.route('/index')
def index():
user = {'username': 'Miguel'}
posts = [
{
'author': {'username': 'John'},
'body': 'Beautiful day in Portland!'
},
{
'author': {'username': 'Susan'},
'body': 'The Avengers movie was so cool!'
}
]
return render_template('index.html', title='Home', user=user, posts=posts)
And the template
<html>
<head>
{% if title %}
<title>{{ title }} - Microblog</title>
{% else %}
<title>Welcome to Microblog</title>
{% endif %}
</head>
<body>
<h1>Hi, {{ user.username }}!</h1>
{% for post in posts %}
<div><p>{{ post.author.username }} says: <b>{{ post.body }}</b></p></div>
{% endfor %}
</body>
</html>
Forms
Introduction
Flask uses Flask-WTF for forms which is a wrapper for WTFForms. To configure this we need a secret key which is attached to requests to help prevent CSRF. This is stored in the root or the project e.g. config.py and loaded by the
import os
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess'
And in __init__.py
from flask import Flask
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
from app import routes
Form Example
Python Code
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired
class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Remember Me')
submit = SubmitField('Sign In')
Template
Pretty basic stuff
{% extends "base.html" %}
{% block content %}
<h1>Sign In</h1>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>
{{ form.username.label }}<br>
{{ form.username(size=32) }}
</p>
<p>
{{ form.password.label }}<br>
{{ form.password(size=32) }}
</p>
<p>{{ form.remember_me() }} {{ form.remember_me.label }}</p>
<p>{{ form.submit() }}</p>
</form>
{% endblock %}
Rendering
from flask import render_template
from app import app
from app.forms import LoginForm
# ...
@app.route('/login')
def login():
form = LoginForm()
return render_template('login.html', title='Sign In', form=form)