Amir Aslam
Amir Aslam

Reputation: 35

Django template syntax error

When i run my django server it shows error but i can't find it.it shows template syntax error

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.

The django template is here:-

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

Upvotes: 3

Views: 948

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

Django tags are surrounded with {% and %} (variables with {{ and }} but let us ignore that for now).

But in your code, except for the first {% if ... %} statement, you consistently write:

{ % endif % }

Notice the space between the { and the %, Django will not parse this as Django tags. You thus should remove the spaces such that the tag reads:

{%  endif  %}

You thus should fix the tags to:

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

I would also advice to avoid writing queries (and other business logic) into templates. Usually this is more the task of the view.

Upvotes: 5

Related Questions