DateField is not rendered as type=“date”

人盡茶涼 提交于 2020-01-24 02:17:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!