Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Sanic meets WTForms

License

NotificationsYou must be signed in to change notification settings

pyx/sanic-wtf

Repository files navigation

Sanic-WTF makes usingWTForms withSanic and CSRF (Cross-Site RequestForgery) protection a little bit easier.

Quick Start

Installation

pip install --upgrade Sanic-WTF

How to use it

Intialization (of Sanic)

fromsanicimportSanicapp=Sanic(__name__)# either WTF_CSRF_SECRET_KEY or SECRET_KEY should be setapp.config['WTF_CSRF_SECRET_KEY']='top secret!'@app.middleware('request')asyncdefadd_session_to_request(request):# setup session

Defining Forms

fromsanic_wtfimportSanicFormfromwtforms.fieldsimportPasswordField,StringField,SubmitFieldfromwtforms.validatorsimportDataRequiredclassLoginForm(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 currentrequest object when you instantiate the form class. Sanic-WTF will do thetrick.

Form Validation

fromsanicimportresponse@app.route('/',methods=['GET','POST'])asyncdefindex(request):form=LoginForm(request)ifrequest.method=='POST'andform.validate():name=form.name.datapassword=form.password.data# check user password, log in user, etc.returnresponse.redirect('/profile')# here, render_template is a function that render template with contextreturnresponse.html(awaitrender_template('index.html',form=form))

Note

For WTForms users: please note that SanicForm requires the whole requestobject instead of some sort of MultiDict.

For more details, please see documentation.

License

BSD New, see LICENSE for details.

Links

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp