Flask wtform DateTimefield渲染问题

时间:2018-04-06 16:45:17

标签: python flask wtforms

使用Flask wtform获得一个奇怪的问题。我在类表单中创建了DateTimeField,但是当我查看页面时,它显示为type="text"

<input id="date" name="date" type="text" value="">

我希望它显示为type="datetime-local",如果您不想手动输入,它会提供一个带有下拉日历和时间搜索的精美输入框。

同样,所有IntegerField,FloatField都是类型文本,其中BooleanField和PasswordField按照应有的方式运行。

也许我在这里遗漏了一些东西。烧瓶和重物是否可能?非常感谢任何指导

这是我的代码:

的index.html

<div class="container">
  <div class="row">
    <div class="col-xs-12">
      <p class="lead">This is the index page  </p>
      {{  form.date() }}
    </div>
  </div>
</div>

script.py

from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, DateTimeField,  BooleanField
from wtforms.validators import Required
from data import ACTORS

app = Flask(__name__)
app.config['SECRET_KEY'] = 'some?bamboozle#string-foobar'
Bootstrap(app)
app.config['BOOTSTRAP_SERVE_LOCAL'] = True

class NameForm(FlaskForm):
    date = DateTimeField('Which date is your favorite?', format='%m/%d/%y', validators=[Required()])

@app.route('/', methods=['GET', 'POST'])
@app.route('/index.html', methods=['GET', 'POST'])
def index():

    form = NameForm()

    return render_template('index.html', form=form)

if __name__ == '__main__':
    app.run(debug=True)

1 个答案:

答案 0 :(得分:1)

核心wtforms DateTimeField只提供文本输入。我想你想要的是:

from wtforms import StringField, BooleanField
from wtforms.fields.html5 import DateTimeLocalField

class NameForm(FlaskForm):
    date = DateTimeLocalField('Which date is your favorite?', format='%m/%d/%y', validators=[Required()])