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.fields 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
Metadata
Release files for Sanic-WTF 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| sanic_wtf-0.7.0.tar.gz | 6.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sanic_wtf-0.7.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.2 kB
Release files / sanic_wtf-0.7.0.tar.gz
| Download URL | sanic_wtf-0.7.0.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d87a1b8b8ff0f5cc4b1c97dc18ab28b9a1682bc803f5a6878aa3ad8c78e429d3
|
|
BLAKE2b-256 checksum How to use checksums |
7cf174329ecd6fa80a0e6998e9828c0b3a816e92ada3c780028dacda64b99ccb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.9.3 CPython/3.11.5
|
Release files / sanic_wtf-0.7.0-py3-none-any.whl
| Download URL | sanic_wtf-0.7.0-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
46e60c97019acb59e21292e21a06f74e09e6260e9bf10caf054f4e09f62cb324
|
|
BLAKE2b-256 checksum How to use checksums |
4f8e48ffe144b08f20305e867751778c56cbd5f12040ac56b02f33b0cd435b98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.9.3 CPython/3.11.5
|
Security for Sanic-WTF 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.