Django - 异常值:if 标签中的表达式意外结束

Dhr*_*ruv 1 django django-templates django-views

我无法弄清楚错误可能是什么。我检查了文档以查看是否有任何语法更改,但我没有找到任何更改。

Unexpected end of expression in if tag.

Template error:
In template /home/dhruv/django-blog/blog/templates/blog/post_detail.html, error at line 5
   Unexpected end of expression in if tag.
   1 : {% extends 'blog/base.html' %}
   2 : 
   3 : {% block content %}
   4 :     <div class="post">
   5 :          {% if post.published_date %} 
   6 :             <div class="date">
   7 :                 {{ post.published_date }}
   8 :             </div>
   9 :         {% elif %}
   10 :             <a class="btn btn-default" href="{% url 'post_publish' pk=post.pk %}">
   11 :                 Publish!
   12 :             </a>
   13 :         {% endif %}
   14 : 
   15 :         {% if user.is_authenticated %}
Run Code Online (Sandbox Code Playgroud)

ha-*_*eul 5

代替:

{% elif %}
Run Code Online (Sandbox Code Playgroud)

{% else %}
Run Code Online (Sandbox Code Playgroud)

************关于 if/else 的文档。

if/else 可以通过以下方式使用:

{% if condition %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

或者

{% if condition1 %}
{% elif condition2 %} # in your case, you are missing condition2
{% endif %}
Run Code Online (Sandbox Code Playgroud)

或者

{% if condition1 %}
{% elif condition2 %} 
{% else %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

或者

{% if condition %}
{% else %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)