0

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

pizzarob
  • 11,711
  • 6
  • 48
  • 69

1 Answers1

4

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>
Ulfric Storm
  • 129
  • 2
  • 12