27π
I managed to make it work. Following the cause of the issue, I hope it can be useful to others.
The HTML <input type='date'>
element wants a date in the format YYYY-mm-dd
; in fact an example of working HTML must be like this:
<input type="date" name="date" value="2020-03-31"
class="form-control dateinput form-control"
placeholder="Select a date" required="" id="id_date">
Since by default the form.DateInput
produces the element <input type='text'>
, it expects a date in the local format: letβs say β31/03/2020β².
Forcing the 'type': 'date'
and local format format=('%d/%m/%Y')
or not passing a format at all, it ignores the value passed since the <input type='date'>
wants format=('%Y-%m-%d')
At last the correct instruction was:
widgets = {
'date': forms.DateInput(
format=('%Y-%m-%d'),
attrs={'class': 'form-control',
'placeholder': 'Select a date',
'type': 'date'
}),
}
0π
Recently, I coded:
date = models.DateTimeField()
inmodels.py
widgets = {'date': NumberInput(attrs={'type': 'date'})}
informs.py
form = <ModelForm>(instance=<model_instance>, data=request.POST)
inviews.py
and ran into the same problem. I figured it out by changing to:
widgets = {'date': DateInput(attrs={'type': 'date'})}
in forms.py
.
Maybe how to pre-fill the form matters in views.py.
Another difference is , I imported DateInput
from django.forms.widgets
, not django.forms
.
- Align radio buttons horizontally in django forms
- How to test a Django on_commit hook without clearing the database?
- Annotate django query if filtered row exists in second table
- How to set timeout for urlfetch in Google App Engine?
- How to specify which eth interface Django test server should listen on?