Django TemplateSyntaxError:“ endblock”,预期为“空”或“ endfor”。您是否忘记注册或加载此标签?

时间:2019-09-18 05:08:24

标签: python django for-loop

我不确定我的语法错误在哪里,如果可以的话,那会很棒。

{% extends 'budget/base.html' %}
{% block content %}
            <ul class="z-depth-1">

            {% for transaction in transaction_list %}
            <li>
                <div class="card-panel z-depth-0 transaction">
                    <div class="row">

                        <div class="col l5">
                            <span class="title"> {{ transaction.title }}</span>
                        </div>

                        <div class="col l5">
                            <span class="title">{{ transaction.amount }}</span>
                        </div>

                        <div class="col l1">
                            <span class="title bold">{{ transaction.category.name }}</span>
                        </div>

                        <a href="">
                            <i class="material-icons right"></i>
                        </a>
                    </div>
                </div>
            </li>
            {% endfor $}


        </ul>
    </section>
</div>
{% endblock content %}

“ budget / base.html”看起来像这样:

{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
      content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>BudgetProject</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

</head>
<body>
    {% block content %}
    {% endblock %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</body>
</html>

我尝试查看类似的问题,并且相当确定for循环的语法正确。我的代码一直有效,直到我添加了{%for x in y%} {%endfor%}

1 个答案:

答案 0 :(得分:5)

您犯了一个小错误,请从{%endfor $}中删除$符号,然后添加%。然后在代码的最后一行中,将{%endblock content%}替换为{%endblock%},这样您的代码将如下所示:

    {% extends 'budget/base.html' %}
{% block content %}
            <ul class="z-depth-1">

            {% for transaction in transaction_list %}
            <li>
                <div class="card-panel z-depth-0 transaction">
                    <div class="row">

                        <div class="col l5">
                            <span class="title"> {{ transaction.title }}</span>
                        </div>

                        <div class="col l5">
                            <span class="title">{{ transaction.amount }}</span>
                        </div>

                        <div class="col l1">
                            <span class="title bold">{{ transaction.category.name }}</span>
                        </div>

                        <a href="">
                            <i class="material-icons right"></i>
                        </a>
                    </div>
                </div>
            </li>
            {% endfor %}


        </ul>
    </section>
</div>
{% endblock %}