wtforms

Flask-WTFform: Flash does not display errors

。_饼干妹妹 提交于 2020-06-09 10:25:49
问题 I'm trying to flash WTForm validation errors. I found this snippet and slightly modified it: def flash_errors(form): """Flashes form errors""" for field, errors in form.errors.items(): for error in errors: flash(u"Error in the %s field - %s" % ( getattr(form, field).label.text, error ), 'error') Here is one of my form classes: class ContactForm(Form): """Contact form""" # pylint: disable=W0232 # pylint: disable=R0903 name = TextField(label="Name", validators=[Length(max=35), Required()])

how to view WTForms validation errors?

时光毁灭记忆、已成空白 提交于 2020-05-15 11:10:48
问题 I am writing some basic tests and have a test failing. def test_new_user_registration(self): self.client.get('/user/register') form = RegistrationForm( email=u'crow@crow.com', first_name=u'Alex', last_name=u'Frazer', username=u'crow', password=u'fake_password', confirm_password=u'fake_password' ) self.assertTrue(form.validate()) The assertion error is failing on form.validate() , but how can I view what the validation errors are? 回答1: Use form.errors: errors A dict containing a list of errors

how to view WTForms validation errors?

核能气质少年 提交于 2020-05-15 11:09:47
问题 I am writing some basic tests and have a test failing. def test_new_user_registration(self): self.client.get('/user/register') form = RegistrationForm( email=u'crow@crow.com', first_name=u'Alex', last_name=u'Frazer', username=u'crow', password=u'fake_password', confirm_password=u'fake_password' ) self.assertTrue(form.validate()) The assertion error is failing on form.validate() , but how can I view what the validation errors are? 回答1: Use form.errors: errors A dict containing a list of errors

Flask WTForms Integerfield type is text instead of number

╄→尐↘猪︶ㄣ 提交于 2020-05-15 06:14:48
问题 This is what I have tried: nrkomp = IntegerField('Number',validators=[NumberRange(min=1, max=5, message='Invalid length')]) In developer tools, this form input has type text and not number, I have read the docs, but could not find a solution to this problem. 回答1: You can use wtforms html5 fields to get html5 input types, and html5 widgets as their associated widgets. from wtforms import Form from wtforms.fields import html5 as h5fields from wtforms.widgets import html5 as h5widgets class F

Flask 教程 第五章:用户登录

女生的网名这么多〃 提交于 2020-04-27 18:25:07
本文翻译自 The Flask Mega-Tutorial Part V: User Logins 这是Flask Mega-Tutorial系列的第五部分,我将告诉你如何创建一个用户登录子系统。 你在 第三章 中学会了如何创建用户登录表单,在 第四章 中学会了运用数据库。本章将教你如何结合这两章的主题来创建一个简单的用户登录系统。 本章的GitHub链接为: Browse , Zip , Diff . 密码哈希 在 第四章 中,用户模型设置了一个 password_hash 字段,到目前为止还没有被使用到。 这个字段的目的是保存用户密码的哈希值,并用于验证用户在登录过程中输入的密码。 密码哈希的实现是一个复杂的话题,应该由安全专家来搞定,不过,已经有数个现成的简单易用且功能完备加密库存在了。 其中一个实现密码哈希的包是 Werkzeug ,当安装Flask时,你可能会在pip的输出中看到这个包,因为它是Flask的一个核心依赖项。 所以,Werkzeug已经安装在你的虚拟环境中。 以下Python shell会话演示了如何哈希密码: 1 >>> from werkzeug.security import generate_password_hash 2 >>> hash = generate_password_hash( ' foobar ' ) 3 >>> hash 4 '

flask总结之session,websocket,上下文管理

寵の児 提交于 2020-03-25 11:39:47
3 月,跳不动了?>>> 1.关于session   flask是带有session的,它加密后存储在用户浏览器的cookie中,可以通过app.seesion_interface源码查看 from flask import Flask,session app = Flask( __name__ ) app.secret_key = ' aptx4869 ' # 必须要指定这个参数 @app.route( ' /login ' ) def login(): # ... # 设置session session[ ' user_info ' ] = ' name ' return ' 123 ' if __name__ == ' __main__ ' : app.run(debug =True) View Code 登录后,通过F12查看网络请求信息,可以看到一个Set-Cookie,这个cookie的key就是session,值为一堆加密字符串 由于服务端是单进程,单线程。所有请求过来时会排队。这个字典会放一个key,这个key就是程序的线程id,value存放用户信息 2.关于websocket    它是一个协议,常与http对比,两者都是应用层协议。websocket主要解决了服务端向客户端推送消息(全双工)   http协议规定:一次请求一次响应,属于无状态短链接

How to pass parameters on onChange of html select with flask-wtf

喜夏-厌秋 提交于 2020-03-18 12:23:04
问题 The following flask code creates a select .. option dropdown menu: model: class SelectForm(Form): country = SelectField('Country', choices=[ ('us','USA'),('gb','Great Britain'),('ru','Russia')]) flask app: @app.route('/new') def new(): form = SelectForm() return render_template('new.html', form = form ) html file: <form method=post action="/register"> {{ render_field(form.country) }} <p><input type=submit value=Register> </form> macro file the defines render_field: {% macro render_field(field

Determine why WTForms form didn't validate

陌路散爱 提交于 2020-03-10 05:00:08
问题 I called form.validate_on_submit() , but it returned False . How can I find out why the form didn't validate? 回答1: For the whole form, form.errors contains a map of fields to lists of errors. If it is not empty, then the form did not validate. For an individual field, field.errors contains a list of errors for that field. The list is the same as the one in form.errors . form.validate() performs validation and populates errors . When using Flask-WTF, form.validate_on_submit() performs an

How to validate a DateField in WTForms

佐手、 提交于 2020-02-25 11:51:07
问题 In my flask app I have a WTForm with two date pickers for a 'start date' and an 'end date'. What is the best way to validate that the 'end date' is not earlier than the 'start date'? from flask_wtf import FlaskForm from wtforms.fields.html5 import DateField from wtforms import SubmitField class Form(FlaskForm): startdate_field = DateField('Start Date', format='%Y-%m-%d') enddate_field = DateField('End Date', format='%Y-%m-%d') submit_field = SubmitField('Simulate') The only thing I found on

Wtforms form field text enlargement

限于喜欢 提交于 2020-02-02 10:56:28
问题 I am new to using wtforms and flask and am trying to enlarge the physical field size and the font inside of it. Should I use a specific piece of code or change it with css. <div class="container"> <div class="col-md-12"> <form id="signinform" class="form form-horizontal" method="post" role="form" style="font-size:24px;"> {{ form.hidden_tag() }} {{ wtf.form_errors(form, hiddens="only") }} {{ wtf.form_field(form.first_name, autofocus=true) }} {{ wtf.form_field(form.last_name) }} {{ wtf.form