Я хочу напечатать время, прошедшее с момента публикации сообщения
У меня есть модель post, которая имеет поле created, поэтому я не могу вывести день создания, но он выходит как полная дата, в то время как все, что мне нужно, это время с тех пор, т.е. (3 часа назад, а не 21-й месяц/год)
class post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
Topic = models.ForeignKey(topic, on_delete=models.CASCADE)
post = models.TextField(max_length=500)
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
liked = models.ManyToManyField(User, related_name='likes', default=None, blank=True)
def __str__(self):
return f'{self.post}'
@property
def num_likes(self):
return self.liked.all().count()
my views.py
def index(request):
posts = post.objects.all().order_by('-created')
topics = topic.objects.all()
comment = comments.objects.all()
return render(request, 'base/home.html', {'posts' : posts, 'topics' : topics, 'comments' : comment})
в моем шаблоне
{% for post in posts %}
<div class="post-body-content">
@{{post.author}} - <small><i>{{post.created}} </i></small> <br>
Topic: <a href="{% url 'topic' post.Topic %}">{{post.Topic}}</a> <br>
<a href="{% url 'post' post.id %}">{{post}}</a> <br>
вы можете использовать теги шаблона timesince в соответствии с документацией django django documentation
{{post.created|timesince}}
например
{% for post in posts %}
<div class="post-body-content">
@{{post.author}} - <small><i>{{post.created|timesince}} </i></small> <br>
Topic: <a href="{% url 'topic' post.Topic %}">{{post.Topic}}</a> <br>
<a href="{% url 'post' post.id %}">{{post}}</a> <br>