Скрытие миллисекунд из timezone.now() (В HTML)
I am building a BlogApp and I am implementing a feature of expiring post so the post will hide in 2 days, So I am showing user the remaining time in post.
Остаточное время отображается отлично, но также отображаются миллисекунды (которые я пытаюсь скрыть)
Вывод такой :-
2 days, 23:43:33.271449
views.py
from django.utils import timezone
from datetime import timedelta
def blogpost_detail(request,blogpost_id):
blog = get_objects_or_404(BlogPost,pk=blogpost_id)
remaining_time = blog.blog_date_added - timezone.now() + timedelta(days=2)
context = {'remaining_time':remaining_time,'blog':blog}
return render(request, 'blogpost_detail.html', context}
Что я пытаюсь показать в качестве вывода :-
Я пытаюсь показать, как :-
2 days 23 Hours Remaining
Что я пробовал :-
- I tried Python/Django timestamp including milliseconds Post's Answer by
dividing 1000but when i dividetimezone by 1000liketimezone.now()/1000then it is keep showing
unsupported operand type(s) for /: 'datetime.datetime' and 'int'
Then i tried hiding
millisecondsfromtemplateby usingtime:"h:i a"like{{ blog.blog_date_added|time:"h:i a" }}But it is showing nothing as output.When i try to add
naturaltimelike{{ blog.blog_date_added|naturaltime }}then it is showing the same output.When i try to add
timesincelike{{ blog.blog_date_added|timesince }}then it is showing :-
'datetime.timedelta' object has no attribute 'year'
Любая помощь будет очень признательна. Thank You in Advance
Я бы посоветовал вам добавить свойство Blog когда истечет срок действия блога, что-то вроде:
from datetime import timedelta
class Blog(models.Model):
# ⋮
@property
def expire_date(self):
return self.blog_date_added + timedelta(days=2)
В шаблоне мы можем затем работать с фильтром шаблона |naturaltime:
{% load humanize %}
{{ blog.expire_date|naturaltime }}