Working on my first django app, and I have a model defined with some DateFields, and then a ModelForm off of that model i.e.
models
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()
pip install django-datetimepicker'datetimepicker' to your INSTALLED_APPSHere is an example of how to use the widget.
. Assign the DateTimePicker to a DateTimeField, DateField or TimeField.
from django import forms
from datetimepicker.widgets import DateTimePicker
class SampleForm(forms.Form):
datetime = forms.DateTimeField(widget=DateTimePicker(),)
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()
}
I had errors when I compiled the code. Better use this one:
from django import forms
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = '__all__'
widgets = {
'my_date': forms.DateInput(attrs={'type': 'date'})
}
start_date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
To use directly in forms.Form
class DateInput(forms.DateInput):
input_type = 'date'
class Gym(forms.Form):
starting_date = forms.DateField(widget = DateInput)