Kasravnd
Kasravnd

Reputation: 107297

"TemplateSyntaxError Invalid block tag: 'trans'" error in Django Templates

After running the runserver command I get the following error:

TemplateSyntaxError at /questions/ Invalid block tag: 'trans'

Does anybody know that what's the reason?

This is my template syntax:

{% extends "two_column_body.html" %}
{# 
    this template is split into several
    blocks that are included here
    the blocks are within directory templates/main_page
    relative to the skin directory

    there is no html markup in this file
#}
<!-- questions.html -->
{% block forejs %}
    {% include "main_page/custom_head_javascript.html" %}
{% endblock %}
{% block title %}{% spaceless %}{% trans %}Questions{% endtrans %}{% endspaceless %}{% endblock %}
{% block content %}
    {% include "main_page/tab_bar.html" %}
    {% include "main_page/headline.html" %}
    {# ==== BEGIN: main_page/content.html === #}
    <div id="question-list">
        {% include "main_page/questions_loop.html" %}
    </div>
    {# ==== END: main_page/content.html === #}
    {% include "main_page/paginator.html" %}
{% endblock %}
{% block sidebar %}
    {% include "main_page/sidebar.html" %}
{% endblock %}
{% block endjs %}
    <script type="text/javascript">
        {# cant cache this #}
        askbot['settings']['showSortByRelevance'] = {{ show_sort_by_relevance|as_js_bool }};
        askbot['messages']['questionSingular'] = '{{ settings.WORDS_QUESTION_SINGULAR|escapejs }}';
        askbot['messages']['answerSingular'] = '{{ settings.WORDS_ANSWER_SINGULAR|escapejs }}';
        askbot['messages']['acceptOwnAnswer'] = '{{ settings.WORDS_ACCEPT_OR_UNACCEPT_OWN_ANSWER|escapejs }}';
        askbot['messages']['followQuestions'] = '{{ settings.WORDS_FOLLOW_QUESTIONS|escapejs }}';
    </script>
    {% include "main_page/javascript.html" %}
    {% include "main_page/custom_javascript.html" %}
{% endblock %}
<!-- end questions.html -->

Upvotes: 18

Views: 35623

Answers (5)

In addition to other answers, you need to put {% load i18n %} after {% extends %} to use {% trans %} or {% translate %} as shown below:

{% extends "two_column_body.html" %}
{% load i18n %}

{% extends %} is explained in Template inheritance as shown below:

  • If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won’t work, otherwise.

So, if you put {% load i18n %} before {% extends %} as shown below:

{% load i18n %}
{% extends "two_column_body.html" %}

Then, the error below occurs:

<ExtendsNode: extends "..."> must be the first tag in the template.

Upvotes: 0

chirinosky
chirinosky

Reputation: 4538

{% trans %}Questions{% endtrans %} is not the correct format.

{% load i18n %} should be at the top of your template, or any extended template using translations.

You can use {% trans "Questions." %}

If you're going to use blocks, they need to be in the format below:

{% blocktrans %}{{ value2translate }}{% endblocktrans %}

More info here.

Upvotes: 40

ripudaman singh
ripudaman singh

Reputation: 31

this is because you have not loaded i18n in this template{% load i18n %} you must add this in each of your template.

Upvotes: 3

Zarkys Salas
Zarkys Salas

Reputation: 131

You must place at the beginning of your extended template code: {% load i18n %} , so you can use the trans Tags:

{% extends 'home/base.html' %}

{% block title %}INICIO{% endblock %}
{% load i18n %}


  {% block opcionesMenu %}
<!-- =====START====== -->
            <a href="#sTop" class="subNavBtn">{% trans "Inicio"  %}</a>
            <a href="#s1" class="subNavBtn">{% trans "Proyectos" %}</a>
            <a href="#s2" class="subNavBtn">{% trans "Diseño Web" %}</a>
            <a href="#s3" class="subNavBtn">{% trans "Marketing" %}</a>
            <a href="#s4" class="subNavBtn">{% trans "Conocenos" %}</a>
            <a href="#s5" class="subNavBtn">{% trans "Contacto" %}</a>
<!-- =====END ====== -->
 {% endblock %}

Upvotes: 2

Yuriy Stetskiv
Yuriy Stetskiv

Reputation: 153

Probably you should use {% blocktrans %}Questions{% endblocktrans %} and you forget to put {% load i18n %} toward the top of your template.

Upvotes: 5

Related Questions