Home > Net >  How to put object datefield in html input value
How to put object datefield in html input value

Time:09-26

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 >
            <label for="birth_date">Data di Nascita:</label>
            <input type="date"  id="birth_date" name="birth_date" value="{{client.birth_date}}" >
        </div>

enter image description here

If i try with, for example:

 </div >
            <label for="birth_date">Data di Nascita:</label>
            <input type="date"  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

CodePudding user response:

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"
  • Related