English 中文(简体)
我可以在决哥的模版里 嵌一个"for"吗?
原标题:Can I nest a "for" in an "if" in a Django template?

我在学习Django, 我试图制造一种情况, 当模板布局已经布局时, 它会列出这些布局, 但如果它不这样做, 它会不这样做, 反而会说没有。

当我在没有布局的模板上只使用“布局...” 语句时,我得到了一个错误信息。

我想到了一些事情, 我创造了一个“如果”的语句, 来首先检查一下页面是否有布局, 如果没有, 一个“else” 语句, 说没有。

但最终结果是,“for”似乎被完全忽略,“else”条件适用于所有页面,不管它们是否有相关的布局。

守则:

{% block header %}
    <h1>The name of this template? It s {{ boilerplate.name }}.</h1>
{% endblock %}

{% block content %}
    <p>&nbsp;</p>
    <p>{{ boilerplate.content }}</p>
    {% if layout in boilerplate.layouts %}
        {% for layout in boilerplate.layouts.all %}
        <p><a href="{{ layout.file.url }}">{{ layout.user }} -- {{ layout.name }} ({{ layout.file.size }})</a>
        {% endfor %}
    {% else %}
    <p>There are no layouts for this template.</p>
    {% endif %}
{% endblock %}

很明显我漏掉了一些东西 可能是非常简单的东西 我错了什么?

最佳回答

如果您在锅炉板.layout {/code> 中的布局是错的 if 标签更容易。

"https://docs.djangoproject.com/en/dev/ref/ref/templates/buildingins/? from=olddocs#f-empty" rel=“nofollow">文件显示 ,您可以在没有布局的情况下使用可选的 prety 标签处理各种情况。重写您的示例代码 :

    {% for layout in boilerplate.layouts.all %}
    <p><a href="{{ layout.file.url }}">{{ layout.user }} -- {{ layout.name }} ({{ layout.file.size }})</a>
    {% empty %}
    <p>There are no layouts for this template.</p>
    {% endfor %}
问题回答

回答问题的主题,是的,当然可以。从你描述问题的方式来看,我猜想,你如何在 boilerplate.layout 中寻找布局可能会有问题。

boilerplate.layout 看起来像是一个关联字段。 因此, 在 < /code> 中进行 < code> in check on that alone 可能是不够的。 在这样的情况下, 我建议用这个代码在外壳或视图中玩耍, 并在视图中看到结果, 因为这将大大有助于调试 。

我简单的猜测是,你需要在锅炉板中做 layout in锅炉板.layouts. all ,但我对你的模型不够熟悉,所以粗略猜想 :)





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签