I am building my first application with Django. I am trying to make a template form to update values in a row in a table in a database. I want to populate all the forms fields with the existing values from the database. I can pass dates in as text just fine:
This displays my date:
<input type="text" class="form-control" value="{{ table.date }}">
but when I try the same value into a date field, it remains blank with I render
This displays a blank date field:
<input type="date" class="form-control" value="{{ table.date }}">
I assume this has to do with handling date formatting. Is there an easy way to handle this in the html? or does it require backend work?
Any python code I tried to write into the template resulted in a 404. Still learning what the capabilities are. From what I have read I am guessing correct way to do this is with a function in my model, but I was hoping I could write something to convert the date in my template.
My view is basic:
def update(request, id):
table = table.objects.get(pk=id)
return render(request, 'app/tempate.html', {'table' : table})
It is a formatting issue as you guessed in your comments. The best approach is to make use of the built-in date template tag
provided by the framework to manipulate the value, instead of doing it in the view:
<input type="date" class="form-control" value="{{ date|date:'Y-m-d' }}">