标签:method error bool strong ide repr parameter 不能 ble
最近学习Flask-web框架,进行到登陆,教程就过于快,自己遇到一个问题要很久才能解决,
BooleanField使用场景:进行选择框,比如:登陆界面,是否记住密码,那个选择框,就可以使用BooleanField来进行操作。
BooleanField用法介绍很少,查不到,现在解决了所以就做这个笔记。
先来引用官方的介绍:
class
wtforms.fields.
BooleanField
(default field arguments, false_values=None)Represents an
<input type="checkbox">
. Set thechecked
-status by using thedefault
-option. Any value fordefault
, e.g.default="checked"
putschecked
into the html-element and sets thedata
toTrue
Parameters: false_values – If provided, a sequence of strings each of which is an exact match string of what is considered a “false” value. Defaults to the tuple (‘false‘, ‘‘)
这句话不是很好理解,不能很很好的知道BooleanField的具体用法,下面使用在StackoverFlow查到的例子:
.py文件
1 from flask import Flask, render_template 2 from flask_wtf import Form 3 from wtforms import BooleanField 4 from wtforms.validators import DataRequired 5 6 app = Flask(__name__) 7 app.secret_key = ‘STACKOVERFLOW‘ 8 9 class ExampleForm(Form): 10 checkbox = BooleanField(‘Agree?‘, validators=[DataRequired(), ]) #这是主要用法 11 12 @app.route(‘/‘, methods=[‘post‘, ‘get‘]) 13 def home(): 14 form = ExampleForm() 15 if form.validate_on_submit(): 16 return str(form.checkbox.data) 17 else: 18 return render_template(‘example.html‘, form=form) 19 20 21 if __name__ == ‘__main__‘: 22 app.run(debug=True, port=5060)
渲染视图文件.html:
1 <form method="post"> 2 {{ form.hidden_tag() }} 3 {{ form.checkbox() }} #这是主要用法 4 <button type="submit">Go!</button> 5 </form> 6 7 <h1>Form Errors</h1> 8 {{ form.errors }}
标签:method error bool strong ide repr parameter 不能 ble
原文地址:http://www.cnblogs.com/liyang93/p/7211065.html