4👍
✅
Let’s say you want to publish a post on 1st December, 2019. So, you’ll save that post with the published
date set to 1st Dec, 2019.
But since that post hasn’t been “published” yet, you don’t want it to appear on your website. So, all you have to do is filter it out and only select those posts whose published
date is before the current time.
Example:
from django.utils import timezone
now = timezone.now() # get the current time
# only select the posts which are `published` before `now`
post_list = post_list.filter(
published__lte=now
).filter(
Q(title__icontains=query) |
Q(content__icontains=query) |
# ... etc ...
).distinct()
Source:stackexchange.com