Django:无效的块标签:'static',预期'endif'

问题描述 投票:0回答:2

我有一个表单,其中我试图在 HTML 中的

if-else
循环之间插入静态图像文件。这是模板的源代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Rango</title>
    </head>

    <body>

    <p>
   <ul>
   {% if user and not user.is_anonymous %}
     <li>
       <a>Hello {{ user.get_full_name|default:user.username }}!</a>
     </li>
     <li>
       <a href="{% url 'auth:logout' %}?next={{ request.path }}">Logout</a>
     </li>
   {% else %}
    <li>
       <a href="{% url 'social:begin' 'twitter' %}?next={{ request.path }}"><img src="{% static "images/twitter.png" %}" /></a>
     </li>
   {% endif %}
   </ul>
 </p>
        <form id="evangelized_form" method="post" action="/rango/fillform/">

            {% csrf_token %}
            {% for hidden in form.hidden_fields %}
                {{ hidden }}
            {% endfor %}

            {% for field in form.visible_fields %}
                {{ field.errors }}
                <b>{{ field.help_text }}</b><br>
                {{ field }}<br><br>
            {% endfor %}

            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>

</html>

但是,这是我遇到的错误:

TemplateSyntaxError at /rango/fillform/
Invalid block tag: 'static', expected 'endif'

如何在 Django 模板中的

if-else
循环之间插入静态图像?

python html django python-2.7
2个回答
14
投票

我相信你需要补充

{% load staticfiles %} 

位于模板的顶部。


0
投票

添加{% load static %}

然后它对我有用

© www.soinside.com 2019 - 2024. All rights reserved.