Displaying the Current Year in a Django Template

Django makes it easy to dynamically display the current year in your templates. This can be useful for copyright notices, showing the release year of your product, or other situations where you want to automatically update the year displayed on your site. In this post, we’ll cover a simple way to show the current year in a Django template.

Using the now Template Tag

Django provides a now template tag that allows you to display the current date and time. To show only the current year, you can use the year filter. Here is an example:

{% now "Y" %}

This will output the four-digit current year. The "Y" indicates to only show the year portion of the date/time.

You can then use this to display a copyright notice or any other text that should contain the current year:

Copyright {% now "Y" %} My Company. All rights reserved.

The output would look something like:

Copyright 2023 My Company. All rights reserved. 

And it will automatically update every year!

Custom Template Tags

While the now template tag works great, you may want to create a custom template tag to wrap the functionality. This keeps your code more reusable and maintainable.

First, inside an application or the project root, create a templatetags directory and an __init__.py file so it is recognized as a Python package. Then create a file like custom_tags.py containing:

from django import template

register = template.Library() 

def current_year():
    return "{0}".format(timezone.now().year)

Here we import Django’s template library and timezone support. We create a template tag function decorated with simple_tag that returns a string containing the current four-digit year.

Then in any template file we can load our custom template tags and use the new current_year tag:

{% load custom_tags %}

Copyright {% current_year %} My Company. All rights reserved.

This keeps the logic for displaying the year separate from the presentation layer in templates.

Considerations and Best Practices

When displaying the current year in Django templates, keep these best practices in mind:

  • Use customTemplate tags for organization and reuse
  • Output the full four-digit year for clarity
  • Update copyright notices and product release years annually
  • Consider Internationalization if applicable
  • Have tests for custom template tags to prevent regressions

Following Django best practices around custom template tags and filters ensures displaying the year dynamically continues working into the future.

Handling i18n translations

If you need to translate the copyright notice or other text containing the year, make sure to properly handle internationalization. Django provides trans template tags to mark text that needs translation.

Here is an example:

{% load i18n %}

{% trans "Copyright" %} {% current_year %} {% trans "My Company. All rights reserved." %}

This keeps the year separate so language translations can position it correctly. The translations would contain placeholders like {year} instead of the actual year.

In Closing

Dynamically showing the current year in Django templates is useful for copyright notices, product release text, displaying ages based on birth year, and any other case where you want to avoid hard-coding a year that will become outdated. Both the built-in now template tag and custom tags provide easy ways to display the current year that will automatically update annually.

Following Django best practices around file organization, custom template tags, and internationalization ensures your method for showing the current year will continue working smoothly into the future. With just a bit of setup, you can avoid outdated years in your templates.