Sanic-WTF - Sanic meets WTForms
Project description
Sanic-WTF makes using WTForms with Sanic and CSRF (Cross-Site Request Forgery) protection a little bit easier.
Quick Start
Installation
pip install --upgrade Sanic-WTF
How to use it
Intialization (of Sanic)
from sanic import Sanic
app = Sanic(__name__)
# either WTF_CSRF_SECRET_KEY or SECRET_KEY should be set
app.config['WTF_CSRF_SECRET_KEY'] = 'top secret!'
@app.middleware('request')
async def add_session_to_request(request):
# setup session
Defining Forms
from sanic_wtf import SanicForm
from wtforms import PasswordField, StringField, SubmitField
from wtforms.validators import DataRequired
class LoginForm(SanicForm):
name = StringField('Name', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
submit = SubmitField('Sign In')
That’s it, just subclass SanicForm and later on passing in the current request object when you instantiate the form class. Sanic-WTF will do the trick.
Form Validation
from sanic import response
@app.route('/', methods=['GET', 'POST'])
async def index(request):
form = LoginForm(request)
if request.method == 'POST' and form.validate():
name = form.name.data
password = form.password.data
# check user password, log in user, etc.
return response.redirect('/profile')
# here, render_template is a function that render template with context
return response.html(await render_template('index.html', form=form))
For more details, please see documentation.
License
BSD New, see LICENSE for details.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sanic_wtf-0.7.0.dev0.tar.gz
(6.7 kB
view hashes)
Built Distribution
Close
Hashes for sanic_wtf-0.7.0.dev0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12ec82db07c9c78e84edd3ebf66d24096c593b77965ffaa6ac72e04c7f28a5c3 |
|
MD5 | a0744aadce6aff3e9a86996234b2652f |
|
BLAKE2b-256 | bd3542fa568e7413fbb3c98919395c79cb1323a69ecb6f2e5e01066db78a8183 |