I'm using flatpickr
for date informations in my Django application. The problem is when I want to update worker
informations, in edit.html
page default value of birth_date
is not displaying correctly. For example, I've created worker Xen
who was born in 01-05-1997, and click to update his name, in birth_date
input it displays today's date. Here are my codes:
forms.py
class WorkerForm(ModelForm):
class Meta:
model = Worker
fields = "__all__"
widgets = {
'birth_date': DateTimeInput(format='%Y-%m-%d %H:%M:%S', attrs={'class':'datetimefield'})
}
models.py
:
class Worker(models.Model):
...
birth_date = models.DateTimeField(blank=True, null=True)
edit.html
:
...
<div class="form-group row">
<label class="col-sm-2 col-form-label">İşçinin doğum tarixi</label>
<div class="col-sm-4">
<input class="datetimefield" type="date" name="birth_date" id="birth_date" value="{{ worker.birth_date }}" />
</div>
</div>
...
<script>
window.addEventListener("DOMContentLoaded", function () {
flatpickr(".datetimefield", {
enableTime: true,
enableSeconds: true,
dateFormat: "Y-m-d H:i:S",
time_24hr: true,
locale: "az",
});
});
</script>
I've changed last line of script according to @Melvyn answer:
defaultDate: `worker.birth_date.strftime()`