TemplateSyntaxError at /admin/ Invalid block tag on line 23: 'translate', expected 'endblock'. Did you forget to register or load this tag?

Viewed 1671

** TemplateSyntaxError at /admin/ Invalid block tag on line 23: 'translate', expected 'endblock'. Did you forget to register or load this tag? Request Method: GET Request URL: http://127.0.0.1:8000/admin/ Django Version: 2.1 Exception Type: TemplateSyntaxError Exception Value:
Invalid block tag on line 23: 'translate', expected 'endblock'. Did you forget to register or load this tag? Exception Location: C:\Users\subramanyam.s.g\PycharmProject\pyyshop\LEARN\lib\site-packages\django\template\base.py, line 522, in invalid_block_tag Python Executable: C:\Users\subramanyam.s.g\PycharmProject\pyyshop\LEARN\Scripts\python.exe Python Version: 3.8.3**

3 Answers

I also happened to come across this error while using Django Version: 2.1.

I was customizing the admin look and feel as per the django documentation 3.1 https://docs.djangoproject.com/en/3.1/intro/tutorial07/.

Django Version: 2.1 can be hosted in a shared hosting domain with reference to https://pythonfusion.com/deploy-django-on-shared-hosting/.

Here is part of the screenshot to just show you i got the same error and see where the problem https://i.stack.imgur.com/RV52y.png

If you have difficulty finding where the Django source files(the ones you want to customise eg admin) are located on your system, use the following procedure:

  1. In your computer locate where your django project is located in the file explorer e.g C:\Users\yourname\Desktop\yourproject.

  2. In your file explorer window, there is a path above your folder showing your current location in the file explorer e.g This PC > Desktop > yourproject. Click inside this path and rewrite cmd and then hit enter key on your keyboard. A command prompt will be opened.

  3. Now if you installed django in a global environment, that is, in local disk without creating a virtual environment enter this code python -c "import django; print(django.__path__)" in the opened command prompt.

  4. If you installed Django into the virtual environment then activate first the virtual environment using still the opened Command prompt, i normally use workon myvirtualenvironmentname to activate my virtual environment. Then paste this code python -c "import django; print(django.__path__)" in the opened command prompt.

  5. Hit enter key on your keyboard and you will see something like this ['C:\\Users\\yourname\\Envs\\yourvirtualenvironmentname\\lib\\site-packages\\django']. This is the location we should arrive to access the Django resources.

  6. Go to your file explorer by pressing CTRL + E on your keyboard.

  7. Navigate to your local disk C, open the Users folder, next open the folder with your name next locate and open the Envs folder and locate your yourvirtualenvironmentname your project is using.

  8. Open your yourvirtualenvironmentname> Lib > site-packages > django > contrib > admin > templates >admin.

  9. You should be here C:\Users\yourname\Envs\yourvirtualenvironmentname\Lib\site-packages\django\contrib\admin\templates\admin in your machine.

Now take key note on the Django version you are using. Where we are currently there are some files that are in Django Version: 2.1 and not in Django Version: 3.1 e.g app_list.html is in Django Version 2.1 and not in 3.1 with respect to this directory where we are.

Note 2. The file names maybe the same but the content inside the file may differ slightly and this is what causes the error

The code below is some of the content in base.html (i have just copied a few of the code where i want to emphasize ) from Django V 3.1.

base.html from Django Version 3.1

`

{% if not is_popup %}
<!-- Header -->
<div id="header">
    <div id="branding">
    {% block branding %}{% endblock %}
    </div>
    {% block usertools %}
    {% if has_permission %}
    <div id="user-tools">
        {% block welcome-msg %}
            {% translate 'Welcome,' %}
            <strong>{% firstof user.get_short_name user.get_username %}</strong>.
        {% endblock %}
        {% block userlinks %}
            {% if site_url %}
                <a href="{{ site_url }}">{% translate 'View site' %}</a> /
            {% endif %}
            {% if user.is_active and user.is_staff %}
                {% url 'django-admindocs-docroot' as docsroot %}
                {% if docsroot %}
                    <a href="{{ docsroot }}">{% translate 'Documentation' %}</a> /
                {% endif %}
            {% endif %}
            {% if user.has_usable_password %}
            <a href="{% url 'admin:password_change' %}">{% translate 'Change password' %}</a> /
            {% endif %}
            <a href="{% url 'admin:logout' %}">{% translate 'Log out' %}</a>
        {% endblock %}
    </div>
    {% endif %}
    {% endblock %}
    {% block nav-global %}{% endblock %}
</div>
<!-- END Header -->
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% translate 'Home' %}</a>
{% if title %} &rsaquo; {{ title }}{% endif %}
</div>
{% endblock %}
{% endif %}

<div class="main shifted" id="main">
  {% if not is_popup and is_nav_sidebar_enabled %}
    {% block nav-sidebar %}
      {% include "admin/nav_sidebar.html" %}
    {% endblock %}
  {% endif %}
  <div class="content">
    {% block messages %}
      {% if messages %}
        <ul class="messagelist">{% for message in messages %}
          <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
        {% endfor %}</ul>
      {% endif %}
    {% endblock messages %}
    <!-- Content -->
    <div id="content" class="{% block coltype %}colM{% endblock %}">
      {% block pretitle %}{% endblock %}
      {% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
      {% block content %}
        {% block object-tools %}{% endblock %}
        {{ content }}
      {% endblock %}
      {% block sidebar %}{% endblock %}
      <br class="clear">
    </div>
    <!-- END Content -->
    {% block footer %}<div id="footer"></div>{% endblock %}
  </div>
</div>
`

I wanted to highlight these lines of code but i was unable. So check of the following in your base.html {% translate 'Welcome,' %}, {% translate 'View site' %}, {% translate 'Documentation' %}, {% translate 'Change password' %}, {% translate 'Log out' %} and {% translate 'Home' %}.

Lets compare the above base.html file with that of Django Version 2.1. Take a close look to discover the difference.

base.html file in Django Version 3.1 `

{% if not is_popup %}
<!-- Header -->
<div id="header">
    <div id="branding">
    {% block branding %}{% endblock %}
    </div>
    {% block usertools %}
    {% if has_permission %}
    <div id="user-tools">
        {% block welcome-msg %}
            {% trans 'Welcome,' %}
            <strong>{% firstof user.get_short_name user.get_username %}</strong>.
        {% endblock %}
        {% block userlinks %}
            {% if site_url %}
                <a href="{{ site_url }}">{% trans 'View site' %}</a> /
            {% endif %}
            {% if user.is_active and user.is_staff %}
                {% url 'django-admindocs-docroot' as docsroot %}
                {% if docsroot %}
                    <a href="{{ docsroot }}">{% trans 'Documentation' %}</a> /
                {% endif %}
            {% endif %}
            {% if user.has_usable_password %}
            <a href="{% url 'admin:password_change' %}">{% trans 'Change password' %}</a> /
            {% endif %}
            <a href="{% url 'admin:logout' %}">{% trans 'Log out' %}</a>
        {% endblock %}
    </div>
    {% endif %}
    {% endblock %}
    {% block nav-global %}{% endblock %}
</div>
<!-- END Header -->
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
{% if title %} &rsaquo; {{ title }}{% endif %}
</div>
{% endblock %}
{% endif %}

{% block messages %}
    {% if messages %}
    <ul class="messagelist">{% for message in messages %}
      <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
    {% endfor %}</ul>
    {% endif %}
{% endblock messages %}

<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
    {% block pretitle %}{% endblock %}
    {% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
    {% block content %}
    {% block object-tools %}{% endblock %}
    {{ content }}
    {% endblock %}
    {% block sidebar %}{% endblock %}
    <br class="clear">
</div>
<!-- END Content -->

{% block footer %}<div id="footer"></div>{% endblock %}
`

You will see the following in your base.html {% trans 'Welcome,' %}, {% trans 'View site' %}, {% trans 'Documentation' %}, {% trans 'Change password' %}, {% trans 'Log out' %} and {% trans 'Home' %}.

To solve your *TemplateSyntaxError at /admin/ Invalid block tag on line 23: 'translate', expected 'endblock'. Did you forget to register or load this tag? *

Just rename the translate to trans and save your file. In conclusion whenever you will add app_list.html or any other file in your admin folder and get and error. Just open the file and rename anything that has translate to trans. This solved my problem.

Incase i miss quoted something for example the django versions remember to let me know. Lets learn together. Thank you

Only way I can come up with for this could happen is if you've created a custom tag module named 'i18n' which would override the builtin i18n module. I can reproduce it by creating a file 'i18n.py' in my app's templatetags/ folder - is that what you have done?

In my case it was a template I've copied and modified. The template was from newer version of django. After upgrading django it all normalised.

Related