I have a class “client” with an attribute datefield called “birth_date”. I want to give the possibility for update this field. How can i put the default value in the form, in the input type date? Because the format in “client.birth_date” is different.
JavaScript
x
5
1
{{client.birth_date}} <br>
2
</div class="form-group">
3
<label for="birth_date">Data di Nascita:</label>
4
<input type="date" class="form-control" id="birth_date" name="birth_date" value="{{client.birth_date}}" >
5
</div>
If i try with, for example:
JavaScript
1
4
1
</div class="form-group">
2
<label for="birth_date">Data di Nascita:</label>
3
<input type="date" class="form-control" id="birth_date" name="birth_date" value="2018-02-01" >
4
</div>
The input value is setting true, but the format is different from the one i get
Advertisement
Answer
Try to format the date like so:
JavaScript
1
2
1
<input type="date" name="birth_date" id="birth_date" value="{{ client.birth_date|date:'Y-m-d' }}">
2
Or set the default date format in settings.py
:
JavaScript
1
2
1
DATE_FORMAT = "Y-m-d"
2