zoukankan      html  css  js  c++  java
  • Flask WTForm BooleanField用法

    最近学习Flask-web框架,进行到登陆,教程就过于快,自己遇到一个问题要很久才能解决,

    BooleanField使用场景:进行选择框,比如:登陆界面,是否记住密码,那个选择框,就可以使用BooleanField来进行操作。

    BooleanField用法介绍很少,查不到,现在解决了所以就做这个笔记。

    先来引用官方的介绍:

    class wtforms.fields.BooleanField(default field argumentsfalse_values=None)

    Represents an <input type="checkbox">. Set the checked-status by using the default-option. Any value for default, e.g. default="checked" puts checked into the html-element and sets the data to True

    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 }}
  • 相关阅读:
    Java
    Java
    Python 浮点数类型的精度问题
    Ubuntu下pip的更新问题
    初章
    第二次结对编程作业
    第一次结对编程作业
    Shengnan的《构建之法》读书笔记
    Backend事后诸葛亮
    ASE Alpha Sprint
  • 原文地址:https://www.cnblogs.com/liyang93/p/7211065.html
Copyright © 2011-2022 走看看