三一世界贸易基金会-三一满足世界贸易基金会
Sanic-WTF的Python项目详细描述
sanic wtf使用带有Sanic和csrf(跨站点请求)的wtforms 伪造)保护起来容易一点。
快速启动
安装
pip install --upgrade Sanic-WTF
如何使用
启动(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
定义表单
fromsanic_wtfimportSanicFormfromwtformsimportPasswordField,StringField,SubmitFieldfromwtforms.validatorsimportDataRequiredclassLoginForm(SanicForm):name=StringField('Name',validators=[DataRequired()])password=PasswordField('Password',validators=[DataRequired()])submit=SubmitField('Sign In')
就这样,只需子类sanicform然后在传入当前 实例化窗体类时请求对象。Sanic WTF将 诡计。
表单验证
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))
注意
对于wtforms用户:请注意sanicform需要整个请求 对象,而不是某种multidict。
有关详细信息,请参阅文档。
许可证
bsd new,请参阅许可证以了解详细信息。