首页 > 解决方案 > 否则在 django 中不起作用,我不知道如何解决这个问题

问题描述

Request Method: GET
Request URL:    http://127.0.0.1:8000/articles/
Django Version: 2.2.7
Exception Type: TemplateSyntaxError
Exception Value:    
Invalid block tag on line 12: 'else', expected 'endblock'. Did you forget to register or load this tag?

{% extends 'base.html' %}

{% block title %}Ultemele statii{% endblock %}

{% block content %}

    {% if latest_articles_list % }

        {% for a in latest_articles_list %}
            <a href="#">{{a.article_title}}</a>
        {% endfor %}
    {% else %}
        statii ne naidena ))=
    {% endif %}

{% endblock %}

代码不起作用

标签: django

解决方案


您的代码看起来不错,除了if语句中的额外空间:

{% extends 'base.html' %}

{% block title %}Ultemele statii{% endblock %}

{% block content %}

    {% if latest_articles_list %}

        {% for a in latest_articles_list %}
            <a href="#">{{a.article_title}}</a>
        {% endfor %}
    {% else %}
        statii ne naidena ))=
    {% endif %}

{% endblock %}

推荐阅读