Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask wtform DateTimefield rendering issue [duplicate]

Getting a weird issue with Flask wtform. I create the DateTimeField in the class form, but when I view the page, it shows as type="text".

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

Where as I would prefer it to show as type="datetime-local", which gives a nice input box with a drop down calendar and time search if you dont want to enter by hand.

Also all IntegerField, FloatField, are type text too, where as BooleanField and PasswordField operate as they should.

Maybe I'm missing something here. Is this possible with flask and wtforms? Any guidance is greatly appreciated

Here is my code:

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)
like image 974
johnfk3 Avatar asked Oct 15 '25 03:10

johnfk3


1 Answers

The core wtforms DateTimeField provides just a text input. I think what you want is:

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()])
like image 70
GAEfan Avatar answered Oct 16 '25 17:10

GAEfan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!