Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the current year in a Django template?

Tags:

python

django

People also ask

How do I get current year in Django?

First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.

What is {% include %} in Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

What does the Django templates contain?

Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.


The full tag to print just the current year is {% now "Y" %}. Note that the Y must be in quotes.


{% now 'Y' %} is the correct syntax


{% now %}


I have used the following in my Django based website http://pmtboyshostelraipur.pythonanywhere.com/ which works fine as expected (Maybe, by the time you're reading this post, it would have stopped working because that's a free hosting so just try in code and see).

{% now 'Y' %}

You can visit & see it in the footer part where I have displayed the current year using the below code(CSS part is omitted so use your own).

<footer class="container-fluid" id="footer">
    <center>
        <p>
           &copy;
           {% now 'Y' %}, 
           PMT Boys hostel <br> 
           All rights reserved
        </p>
    </center>
</footer>

And it is displaying the following centred text in my website's footer.

©2018, PMT Boys hostel 
All rights reserved