Django form.as_p DateField not showing input type as date
start_date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
You can create a custom widget:
from django import forms
class DateInput(forms.DateInput):
input_type = 'date'
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = '__all__'
widgets = {
'my_date': DateInput()
}
There's no need to subclass DateInput
.
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = '__all__'
widgets = {
'my_date': forms.DateInput(attrs={'type': 'date'})
}
Using the django-widget-tweaks package you can do this pretty simply by using:
{% load widget_tweaks %}
{{form.date|attr:"type:date"}}
and making the field a date time field in your class:
date = forms.DateField()