Django模板语法错误

时间:2018-08-28 16:39:20

标签: python django django-templates

当我运行django服务器时,它显示错误但我找不到它。它显示模板语法错误

Accounting::Invoice.includes(:contact)
                   .only(<list_of_attributes>)
                   .page(params[:page].to_i)

django模板在这里:-

Unclosed tag on line 2: 'if'. Looking for one of: elif, else, endif.
Request Method: GET
Request URL:    http://127.0.0.1:8000/tag/django/
Django Version: 2.0.5
Exception Type: TemplateSyntaxError
Exception Value:    
Unclosed tag on line 2: 'if'. Looking for one of: elif, else, endif.
Exception Location: C:\Users\user\Anaconda3\envs\amirdjango\lib\site-packages\django\template\base.py in unclosed_block_tag, line 549
    Python Executable:  C:\Users\user\Anaconda3\envs\amirdjango\python.exe
    Python Version: 3.6.5
    Python Path:    
    ['H:\\Amir\\Django\\myDjangostuff\\suorganizer',
     'C:\\Users\\user\\Anaconda3\\envs\\amirdjango\\python36.zip',
     'C:\\Users\\user\\Anaconda3\\envs\\amirdjango\\DLLs',
     'C:\\Users\\user\\Anaconda3\\envs\\amirdjango\\lib',
     'C:\\Users\\user\\Anaconda3\\envs\\amirdjango',
     'C:\\Users\\user\\Anaconda3\\envs\\amirdjango\\lib\\site-packages']
    Server time:    Tue, 28 Aug 2018 15:59:01 +0000

    -Error: Unclosed tag on line 2: 'if'. Looking for one of: elif, else, endif.

1 个答案:

答案 0 :(得分:4)

Django标签被{%%}包围(变量为{{}},但现在我们忽略它)。

但是在您的代码中,除了第一个{% if ... %}语句以外,您始终在编写:

{ % endif % }

请注意{%之间的空格,Django不会将其解析为Django标签。因此,您应该删除空格,以使标签显示为:

{%  endif  %}

因此,您应该将标签固定为:

<h2> {{ tag.name|title }} </h2>
 {% if tag.startup_set.all %}
 <section>
 <h3>Startup {{ tag.startup_set.count|pluralize }}</h3>
 <p>
 Tag is associated with
 {{ tag.startup_set.count }}
 startup {{ tag.startup_set.count|pluralize }}
</p>
 <ul>
 {% for startup in tag.startup_set.all %}
 <li><a href="">
 { { startup.name } }
 </a></li>
 {% endfor %}
 </ul>
 </section>
 {% endif %}
 {% if tag.blog_posts.all %}
 <section>
 <h3>Blog Post { { tag.blog_posts.count|pluralize } } </h3>
 <ul>
 {% for post in tag.blog_posts.all %}
 <li><a href="">
 { { post.title|title } }
 </a></li>
 {% endfor %}
 </ul>
 </section>
 {% endif %}
 {% if not tag.startup_set.all and not tag.blog_posts.all %}
 <p>This tag is not related to any content.</p>
 {% endif %}

我也建议避免将查询(和其他业务逻辑)写入模板。通常,这更多是 view 的任务。