Python 如何在Django Tempates中处理来自Jinja2的for/if循环

Python 如何在Django Tempates中处理来自Jinja2的for/if循环,python,django,jinja2,Python,Django,Jinja2,在使用django模板的遗留项目中工作。 如何将此for/if语法从Jinja2翻译为django: 此Jinja示例按预期工作,但django中出现语法错误: {% for story in stories if story.status == "draft" %} <h1> {{story.title}} </h1> {% empty %} <p> No drafts to see here</p> {% endfor %

在使用django模板的遗留项目中工作。 如何将此for/if语法从Jinja2翻译为django:

此Jinja示例按预期工作,但django中出现语法错误:

 {% for story in stories if story.status == "draft"  %}
    <h1> {{story.title}} </h1>
 {% empty %}
   <p> No drafts to see here</p>
 {% endfor %}

 {% for story in stories if story.status == "published"  %}
    <h1> {{story.title}} </h1>
 {% empty %}
   <p> No published stories to see here</p>
 {% endfor %}

django模板系统有一些文档:

这应该做到:

{% if stories|length %}
    {% for story in stories %}
        {%  if story.status == "published" %}
            <h1> {{story.title}} </h1>
        {% endif %}
    {% endfor %}
{% else %}
    <p>Nothing to see here.</p>
{% endif %}
{%if故事|长度%}
{故事%中的故事%}
{%if story.status==“published”%}
{{story.title}
{%endif%}
{%endfor%}
{%else%}
这里没什么可看的

{%endif%}
我已经更新了这个问题,因为我认为我错误地传达了这个问题。故事可能有长度,但在按发布/草稿状态筛选时没有长度,我想不出一个“模板”解决方案。但是你可以做一个“自定义过滤器”,或者直接将发布的故事传递给模板。
{% if stories|length %}
    {% for story in stories %}
        {%  if story.status == "published" %}
            <h1> {{story.title}} </h1>
        {% endif %}
    {% endfor %}
{% else %}
    <p>Nothing to see here.</p>
{% endif %}