django TemplateSyntaxError无效的块标记:'trans'

时间:2014-06-27 12:07:52

标签: django django-templates

运行runserver命令后,出现以下错误:

  

在/ questions /无效的块标签上的TemplateSyntaxError:'trans'

有人知道是什么原因吗?

这是我的模板语法:

     {% 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 -->

4 个答案:

答案 0 :(得分:25)

{% trans %}Questions{% endtrans %}格式不正确。

{% load i18n %}应位于模板的顶部,或使用翻译的任何扩展模板。

您可以使用{% trans "Questions." %}

如果您要使用块,则需要采用以下格式:

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

更多信息here

答案 1 :(得分:4)

您可能应该使用{% blocktrans %}Questions{% endblocktrans %}而忘记将{% load i18n %}放在模板的顶部。

答案 2 :(得分:2)

您必须放置在扩展模板代码的开头:{% load i18n %},以便您可以使用trans标签:

{% 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 %}

答案 3 :(得分:2)

这是因为您尚未在此模板{% load i18n %}中加载i18n,您必须在每个模板中添加此内容。