Skip to content
Advertisement

How to put object datefield in html input value

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.

{{client.birth_date}} <br>
        </div class="form-group">
            <label for="birth_date">Data di Nascita:</label>
            <input type="date" class="form-control" id="birth_date" name="birth_date" value="{{client.birth_date}}" >
        </div>

enter image description here

If i try with, for example:

 </div class="form-group">
            <label for="birth_date">Data di Nascita:</label>
            <input type="date" class="form-control" id="birth_date" name="birth_date" value="2018-02-01" >
        </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:

<input type="date" name="birth_date" id="birth_date" value="{{ client.birth_date|date:'Y-m-d' }}">

Or set the default date format in settings.py:

DATE_FORMAT = "Y-m-d"
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement