问题
class Form(Form):
plan_start = DateField('Plan Start', validators=[Required()])
this code will render this html.
<input id="plan_start" name="plan_start" type="text" value="">
My question is: why the type is text
and not date
?
I only can get this resolved by passing explicitly the type='date'
in template.
{% raw form.plan_start.label %}{% raw form.plan_start(type='date') %}
回答1:
You can use the DateField from html5.
from wtforms.fields.html5 import DateField
class Form(Form):
plan_start = DateField('Plan Start', validators=[Required()])
回答2:
To get type as date instead of text , you need to pass format option also like below
password = DateField('Your Date of Birth', validators=[DataRequired()], format='%Y-%m-%d')
回答3:
Try to create a custom widget:
from django.forms import widgets
class CustomDateInput(widgets.TextInput):
input_type = 'date'
class Form(Form):
plan_start = DateField('Plan Start', validators=[Required()], widget=CustomDateInput)
来源:https://stackoverflow.com/questions/19588393/datefield-is-not-rendered-as-type-date