I put a DateField inside a ModelForm, which seems to work fine, if I add new entries to my database. Then I added a page, where I can edit one of those entries. Of course, I am passing the date of the desired entry to the HTML, but the date format is not set as expected.
# models.py
class SomeModel(models.Model):
someDate = models.DateField()
description = models.CharField(max_length=100)
# forms.py
class SomeForm(forms.ModelForm):
someDate = forms.DateField(input_formats=["%d.%m.%y"], label="Some Date")
class Meta:
model = SomeModel
widgets = {
"someDate": forms.DateInput(format=("%d.%m.%y"))
}
# views.py
def someModel_edit(request: HttpRequest, id: int):
# ...
query = SomeModel.objects.get(id=id)
form = SomeForm(instance=query)
args = {
'form': form
}
return render(request, 'SomeHtml.html', args)
{% extends 'base.html' %}
{% block content %}
<h2>Edit Model</h2>
<form action="{% url 'someModel_edit' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" name="edit-someModel" value="Edit">
</form>
{% endblock %}
I expect the format of the date in the edit page to be as I configured in the forms.py file (e.g. 12.08.2024). Sadly it always looks like this:
And even worse, if I edit the description of the model entry and hit save, the software tells me, that I use the wrong date format.