如何使用Flask-WTF表单实现日期选择器

时间:2019-03-29 14:44:51

标签: python-3.x flask-wtforms

使用Flask-wtf应该如何实现日期选择器?

曾尝试使用网络上的各种示例,但似乎都没有用。

查看代码以获取最新尝试。

from flask import Flask, render_template, session, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_wtf import FlaskForm, Form
from wtforms.fields.html5 import DateField
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from wtforms.fields import DateField
from flask_datepicker import datepicker

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'

bootstrap = Bootstrap(app)
moment = Moment(app)

class LoginForm(FlaskForm):
    entrydate = DateField('entrydate')
    submit = SubmitField('Submit')


@app.route('/date', methods=['GET', 'POST'])
def datep():
    form = LoginForm()
    if form.validate():
        return 'Form Successfully Submitted!'
    return render_template('date.html', form=form)

模板文件:

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}Flasky{% endblock %}

{% block page_content %}
<div class="page-header">
    <h1>Hello, select date!</h1>
</div>
{{ wtf.quick_form(form) }}
{% endblock %}

预期:查看下拉日期选择器。

实际:不显示日期选择器。

1 个答案:

答案 0 :(得分:0)

在 FlaskForm 中,它将如下所示:

    #Import the resource from wtforms.fields.html5
    from wtforms.fields.html5 import DateField,DateTimeField 
   
    class LoginForm(FlaskForm):
    entrydate = DateField('entrydate', format='%Y-%m-%d' )
    submit = SubmitField('Submit')

您可以尝试将以下内容放入 html:

<div class="page-header">
    <h1>Hello, select date!</h1>
 {{form.entrydate (class= "datepicker")}}
</div>

HTML5 的其余部分是这样的:

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}Flasky{% endblock %}

{% block page_content %}
<div class="page-header">
    <h1>Hello, select date!</h1>
    {{form.entrydate (class= "DateTimeField")}}

</div>
{{ wtf.quick_form(form) }}
{% endblock %}