Django模板语法错误

3
当我运行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.

这里是Django模板:

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

1
您将 {% tag %}(有效)与 { %(无效)混合使用了。 - Willem Van Onsem
1个回答

5

Django标签以{%%}包围(变量用{{}},但暂时先忽略)。

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

{ % endif % }

请注意 {% 之间的空格,Django 不会将其解析为 Django 标签。因此,您应该删除这些空格,使标签变为:{%
<b>{%</b>  endif  <b>%}</b>

你需要将标记修正为以下内容:

因此,你应该将标签修改为:

<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>
 <b>{% for startup in tag.startup_set.all %}</b>
 <li><a href="">
 { { startup.name } }
 </a></li>
 <b>{% endfor %}</b>
 </ul>
 </section>
 <b>{% endif %}</b>
 <b>{% if tag.blog_posts.all %}</b>
 <section>
 <h3>Blog Post { { tag.blog_posts.count|pluralize } } </h3>
 <ul>
 <b>{% for post in tag.blog_posts.all %}</b>
 <li><a href="">
 { { post.title|title } }
 </a></li>
 <b>{% endfor %}</b>
 </ul>
 </section>
 <b>{% endif %}</b>
 <b>{% if not tag.startup_set.all and not tag.blog_posts.all %}</b>
 <p>This tag is not related to any content.</p>
 <b>{% endif %}</b>

我建议避免在模板中编写查询(和其他业务逻辑)。通常这是视图的任务。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接