小编ohi*_*hid的帖子

“照片”属性没有与之关联的文件

Template error:
Run Code Online (Sandbox Code Playgroud)

在模板 D:\virtualEnv\alumni\member\templates\member\index.html 中,第 15 行出错

The 'photo' attribute has no file associated with it.   5 :     <!-- Albums -->
   6 :     <div class="row">
   7 :         <div class="col-sm-12">
   8 :             <h3>{{ user.username }}'s Albums</h3>
   9 :         </div>
   10 :         {% if all_persons %}
   11 :             {% for person in all_persons %}
   12 :                 <div class="col-sm-4 col-lg-2">
   13 :                     <div class="thumbnail">
   14 :                         <a href="{% url 'member:detail' person.id %}">
   15 :                             <img src=" {{ person.photo.url }} " class="img-responsive">
   16 : …
Run Code Online (Sandbox Code Playgroud)

django templates image django-models

3
推荐指数
1
解决办法
1万
查看次数

如何在django模板中同时检查if、elif、else条件

我在类别字段中有 3 个类别。我想在 django 模板中检查它并为 3 个不同的类别分配适当的 url。

我试过:

{% for entry in entries %}
        {% if entry.category == 1 %}
        <a href="{% url 'member:person-list' %}"><li>{{ entry.category }}</li></a>
        {% elif entry.category == 2 %}          
        <a href="{% url 'member:execomember-list' %}"><li>{{ entry.category}}</li></a>
         {% else %}
         <a href="{% url 'member:lifemember-list' %}"><li>{{ entry.category}}</li></a>
        {% endif %}
{% empty %}
    <li>No recent entries</li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

但我知道python只用if检查第一个匹配条件。因此它只给出了一个想要的结果。如何使用正确的链接获取所有三个条目?

编辑:

虽然python只检查第一个匹配的if条件,但当在for循环中使用elif时,它会检查每个条件直到endfor循环。因此,我在下面的回答工作正常。

python django

-3
推荐指数
1
解决办法
2万
查看次数

标签 统计

django ×2

django-models ×1

image ×1

python ×1

templates ×1