0

I have a base.html template file for Django (4.1.2) as:

<!DOCTYPE html>
<html lang="en">
    {% load static %}
    {% load i18n %}
  <head>
    <meta charset="utf-8">
      {% block title %}
        <title>My Title</title>
      {% endblock %}
   
     </head>
  <body>
    {% block content %}

    {% endblock content %}
  </body>
</html>

and an index.html page, at the same level in the /templates folder of my app, extending the base one, as:

{% extends "base.html" %}

{% block content %}
  <h1>My Django project</h1>
    <ul>
      <li><a href="/admin">{% trans "Admin" %}</a></li>
      <li><a href="{% url 'foo' %}">{% trans "Foo" %}</a></li>
    </ul>
{% endblock %}

But when I browse the latter page, the server returns the following error:

django.template.exceptions.TemplateSyntaxError:
  Invalid block tag on line 6:
    'trans', expected 'endblock'.
    Did you forget to register or load this tag?

But if I simply add {% load i18n %} at the second line of the index.html, the page loads fine.

What is wrong with the loading of the base template in the index.html page?

This doesn't help as it doesn't differentiate the behaviour encountered here with the fact that loading, e.g. {% load django_bootstrap5 %} in base.html is working very well through all child pages without having to ever specify it again in those pages.

swiss_knight
  • 5,787
  • 8
  • 50
  • 92
  • Does this answer your question? [Is it possible to load a custom template tag in base and use it in extented templates](https://stackoverflow.com/questions/20560222/is-it-possible-to-load-a-custom-template-tag-in-base-and-use-it-in-extented-temp) – Abdul Aziz Barkat Nov 05 '22 at 14:38

2 Answers2

0

you must always load any tag library in each page if you want to use it.Even if the you have already load it in parent page.

  • Hmm, okay, but then how do you explain that loading, e.g. `{% load django_bootstrap5 %}` in `base.html` is working very well throughout all child pages without having to ever specify it again? – swiss_knight Nov 05 '22 at 14:46
  • @s.k to answer that do you actually use "django_bootstrap5" in all of your child templates? – Abdul Aziz Barkat Nov 05 '22 at 14:51
  • No, I do not. But I actually see layout changes in all pages extending the base.html template. And `{% load django_bootstrap5 %}` is only specified once; in this base.html template. But for `i18n` it doesn't seem to work the same way. – swiss_knight Nov 05 '22 at 15:09
  • @s.k Well the layout changes will happen in all the templates because it's CSS... – Abdul Aziz Barkat Nov 05 '22 at 15:23
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '22 at 13:22
0

I found a way to globally enable i18n in settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            # ... some options here ...
        },
        'builtins': ['django.templatetags.i18n'], # <---- add this line
    },
]

Then, apparently, you no more need to load this template tag in every html template. It is not even necessary in base.html anymore.

More on templates built-in backends: https://docs.djangoproject.com/en/4.1/topics/templates/#module-django.template.backends.django

And on writing {% load i18n %} only once: https://code.djangoproject.com/ticket/1193

Inspired by: Load a Django template tag library for all views by default

swiss_knight
  • 5,787
  • 8
  • 50
  • 92