如何在Django模型中创建失效日期? [英] How to I create an expiration date in my django model?

查看:47
本文介绍了如何在Django模型中创建失效日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何为仅在未过期时显示的新帖子创建过期日期选项?

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

推荐答案

在您的模型中,您需要这样的东西:

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})

模板:

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

这篇关于如何在Django模型中创建失效日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆