问题
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