15
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') %}

3 Answers 3

30

You can use the DateField from html5.

from wtforms.fields.html5 import DateField


class Form(Form):
    plan_start = DateField('Plan Start', validators=[Required()])
3
  • 4
    This does not fix the issue. Dec 15, 2018 at 2:40
  • ran into this error and this fixed the issue for me. Thanks! Sep 4, 2020 at 15:33
  • 1
    @SeanPianka This is the documented and the correct way to do it. The fields.DateField as being used in the question only renders the text input (see source code). To render a date input, you need to use fields.html5.DateField as shown in the answer. This will change in version 3 (yet unreleased): DateField will render a date input by default.
    – xyres
    Sep 10, 2021 at 17:25
0

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) 
1
  • 2
    Django? I am not using django.
    – anvd
    Oct 25, 2013 at 13:51
0

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')
1
  • Not only does this not work, an HTML5 <input type='date' /> element can't have its format set for it. It depends on the browser, or the OS.
    – mlibby
    Jan 27, 2020 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.