【问题标题】:django TemplateSyntaxError Invalid block tag: 'trans'django TemplateSyntaxError 无效的块标签:'trans'
【发布时间】:2014-08-18 13:02:26
【问题描述】:

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

TemplateSyntaxError at /questions/ Invalid block tag: '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 -->

【问题讨论】:

  • 你忘记{% load i18n %}了吗?

标签: django django-templates


【解决方案1】:

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

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

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

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

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

更多信息here

【讨论】:

  • 你检查过其他模板吗?您能否提供调试文件的输出?
  • {% load i18n %} 应该在当前模板的顶部。**和所有扩展模板** !!!也必须使用{% blocktrans %}Qusetion{% endblocktrans %} 而不是{% rans %}Qusetion{% endtrans %} 感谢您的帮助!
  • 很高兴你能成功。我将更新答案以澄清{% load i1in %} 部分。
  • 链接已失效,请修改或删除参考链接
【解决方案2】:

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

【讨论】:

  • 尝试使用{% blocktrans %}Questions{% endblocktrans %} 代替{% trans %}Questions{% endtrans %}{% trans "Questions" %} 并删除{% endtrans %}
【解决方案3】:

这是因为你没有在这个模板中加载 i18n{% load i18n %} 你必须在你的每个模板中添加这个。

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-06-28
      • 2016-04-06
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 2016-01-26
      • 2015-02-10
      • 2012-03-21
      相关资源
      最近更新 更多