Timefield presentation format in Django

Asked

Viewed 577 times

1

class Passagem(models.Model):
    inscricao = models.ForeignKey(Inscricao, verbose_name='inscricao', related_name='passagem',
                              on_delete=models.CASCADE)
    hora_passagem = models.TimeField('Tempo', auto_now_add=True)

I have this table where you record hora_passagem, who’s kind TimeField. When displaying on the web system, it displays the following format: "14:35".

How do I present in this format : "14:34:58.943943"

I’m using Django 2.1.

1 answer

3


In Django there is the filter in date in the template you can use to format the date:

{% for obj in objs %}
    <h1>{{ obj.hora_passagem|date:'H:i:s:u' }}</h1>
{% endfor %}

This for a model similar to:

from django.db import models

class Passagem(models.Model):
    hora_passagem = models.TimeField()

And a view:

from django.shortcuts import render
from .models import Passagem

# Create your views here.
def home(request):
    objs = Passagem.objects.all()
    return render(request, 'main/index.html', {'objs': objs})

See working in https://LopsidedFumblingTypes--acwoss.repl.co or the source code on Repl.it.

  • Perfect, I managed to make it appear the way I wanted. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.