Show date & time from database in html input field

Question:

I wanted to show dates from my database in my HTML input field. For example id one has "2023-01-13 17:30:00", but when I display it on my input field it just show "Jan." I wanted it to display exactly as "2023-01-13 17:30:00". You might be confused why there is a submit button but has readonly input fields, it’s for a condition where if the user is a not the admin, they can’t edit the records.

HTML file

<div class="card-body">
    <div class="form-group">
        <label >Title:</label>
            <input type="text" name="title" value={{query.title}} readonly>
    </div>
    <div class="form-group">
        <label>Description:</label>
            <input type="text" name="description" value={{query.description}} readonly>
    </div>
    <div class="form-group">
        <label>Start time:</label>
            <input type="text" name="start_time" value={{query.start_time}} readonly>
    </div>
    <div class="form-group">
        <label>End time:</label>
            <input type="text" name="end_time" value={{query.end_time}} readonly>
    </div>
    <div align="center" >
        <button type="submit" class="btn btn-primary">Submit</button>
        <button type="reset" class="btn btn-primary">Refresh</button>
    </div>
</div>

views.py

def event_edit(request, id):
    if request.method == 'GET':
        query = Event.objects.get(id=id)
        return render(request, 'project/event_edit.html', {'query': query})

models.py

class Event(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
Asked By: krisssz

||

Answers:

You can use template tag for date-time like this

<input type="text" name="end_time" value={{i.cr_date|date:"d-m-Y,H:i:s"}} readonly>

Browser Output

enter image description here

for more info visit here

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.