How to display current year in a django template ?

Published: August 13, 2024

Tags: Django;

DMCA.com Protection Status

Introduction

To display the current year in a Django template, you can use the built-in template tags and filters. There are two common ways to achieve this:

Using Django Template Tags

You can use the {% now %} template tag to display the current year. Here's an example:

1
{% now "Y" %}

This will render the current year in four digits, like 2024.

Using a Context Processor

If you want the current year to be available in every template without having to add it manually, you can create a context processor. Here's how you can do it:

Create a context processor:

Create a file called context_processors.py in one of your Django apps (or in a dedicated utility app).

1
2
3
4
5
# myapp/context_processors.py
from datetime import datetime

def current_year(request):
    return {'current_year': datetime.now().year}

Add the context processor to your settings:

Add the path to the context processor in your TEMPLATES setting.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                # Add your custom context processor here
                'myapp.context_processors.current_year',
            ],
        },
    },
]

Use it in your template:

Now, you can use {{ current_year }} in any template.

1
<footer>&copy; {{ current_year }}</footer>

This will automatically render the current year wherever you use the {{ current_year }} variable in your templates.

To add a "Copyright © [current year] - All rights reserved" statement at the end of each page in a Django project, you can follow these steps:

Use a Base Template

If your project uses a base template that all other templates extend, you can add the copyright statement directly in the base template. This ensures that it appears on every page.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<!-- base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}My Website{% endblock %}</title>
    {% block head %}{% endblock %}
</head>
<body>
    {% block content %}{% endblock %}

    <!-- Footer Section -->
    <footer>
        <p>Copyright © {% now "Y" %} - All rights reserved.</p>
    </footer>
</body>
</html>

Using a Context Processor (Optional)

If you prefer to use a context processor to make the current year available throughout your templates, you can do the following (as mentioned earlier):

Create the context processor:

1
2
3
4
5
# myapp/context_processors.py
  from datetime import datetime

  def current_year(request):
      return {'current_year': datetime.now().year}

Add the context processor to your settings:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'myapp.context_processors.current_year',
            ],
        },
    },
]

Update your base template:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<!-- base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}My Website{% endblock %}</title>
    {% block head %}{% endblock %}
</head>
<body>
    {% block content %}{% endblock %}

    <!-- Footer Section -->
    <footer>
        <p>Copyright © {{ current_year }} - All rights reserved.</p>
    </footer>
</body>
</html>

Extending the Base Template

If you have multiple pages that extend from the base template, the footer with the copyright statement will automatically be included.

Example:

1
2
3
4
5
6
7
8
9
<!-- home.html -->
{% extends "base.html" %}

{% block title %}Home Page{% endblock %}

{% block content %}
<h1>Welcome to My Website</h1>
<p>This is the home page content.</p>
{% endblock %}

With this setup, every page that extends base.html will have the "Copyright © [current year] - All rights reserved." footer at the bottom.

Creative Commons License

This work is free of known copyright restrictions.