How to I create an expiration date in my django model?

戏子无情 提交于 2021-02-08 11:48:20

问题


How would I create an expiration date option for new posts that would only show when they are unexpired?


回答1:


In your model you need something like this:

expiration_date = models.DateTimeField()

Then in your views you could access the database like this:

def UnexpiredPosts(request):
    unexpired_posts = YourPostModelName.objects.filter(expiration_date__gt = datetime.now())

    return render(request, "path_to_template", {'Posts': unexpired_posts})

The template:

<ul>
{% for i in Posts %}
<li>{{ i }}</li>
{% endfor %}
</ul>


来源:https://stackoverflow.com/questions/19338357/how-to-i-create-an-expiration-date-in-my-django-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!