Skip to content

Displaying Content Based on Date in Django

From time to time we need content in a template to show from a certain date. An example would be a news article that needs to go live from the 4th of July, 2023.

Here’s how to do it simply in Django:

In your template add this code to create a template variable:

{% now "Ymd" as current_date %}

Then you can use a simple if statement to show your content:

{% now "Ymd" as current_date %}
{% if current_date >= "20230704" %}
    Show this content!
{% endif %}

I hope this little piece of code helps you!

Leave a Reply