Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django博客没有正常循环_Python_Django_Loops_Django Models_Django Templates - Fatal编程技术网

Python Django博客没有正常循环

Python Django博客没有正常循环,python,django,loops,django-models,django-templates,Python,Django,Loops,Django Models,Django Templates,我用Django创建了一个博客,我最初在每篇文章中循环使用: <div class="container"> <h2 class="latest-posts">Latest Posts</h2> <hr /> {% for post in posts.all %} <h4><a href="{% url 'post_detail' post.id %}">{{ post.title }}</a></h

我用Django创建了一个博客,我最初在每篇文章中循环使用:

<div class="container">

<h2 class="latest-posts">Latest Posts</h2>
<hr />

{% for post in posts.all %}

<h4><a href="{% url 'post_detail' post.id %}">{{ post.title }}</a></h4>
<i class="fa fa-calendar" aria-hidden="true"></i> {{ post.pub_date_pretty }}

<img src="{{ post.image.url }}" class="img-responsive center-sm-block" style='width:75%; text-align:center;' />
<br/>
<p>{{ post.summary }}</p>
<br/>
<br/>

{% endfor %}

</div>

最新帖子

{posts.all%} {{post.pub_date_pretty}}
{{post.summary}}



{%endfor%}
真是太棒了!但我不只是想有一个线性的帖子列表,而是想在一张引导卡中显示每个独特的帖子,我尝试使用这个解决方案:

<div class="container">
    <h2 class="latest-posts">Latest Posts</h2>
    <hr />

    {% for post in posts.all %}

    <div class="row">

        <div class="col-12 col-md-6">
            <!-- Card -->
            <article class="card animated fadeInRight">
                <div class="card-block">
                    <h4 class="card-title"><a href="{% url 'post_detail' post.id %}">{{ post.title }}</a></h4>
                    <h6 class="text-muted">Antoine de Saint-Exupéry</h6>
                    <p class="card-text">{{ post.summary }}</p>
                </div>
                <div class="card-block text-center">
                    <div class="btn-group hidden-sm-down hidden-md-down" role="group" aria-label="Card buttons">
                        <a href="#" class="card-link">Read more</a>
                    </div>
                </div>
                <img class="card-img-bottom img-responsive" style='width:100%; text-align:center;' src="{{ post.image.url }}" alt="White sand" />
            </article><!-- .end Card -->
        </div>

        <div class="col-12 col-md-6">
            <!-- Card -->
            <article class="card animated fadeInRight">
                <div class="card-block">
                    <h4 class="card-title"><a href="{% url 'post_detail' post.id %}">{{ post.title }}</a></h4>
                    <h6 class="text-muted">Antoine de Saint-Exupéry</h6>
                    <p class="card-text">{{ post.summary }}</p>
                </div>
                <div class="card-block text-center">
                    <div class="btn-group hidden-sm-down hidden-md-down" role="group" aria-label="Card buttons">
                        <a href="#" class="card-link">Read more</a>
                    </div>
                </div>
                <img class="card-img-bottom img-responsive" style='width:100%; text-align:center;' src="{{ post.image.url }}" alt="White sand" />
            </article><!-- .end Card -->
        </div>

    </div>

    {% endfor %}

</div><!-- End container -->

最新帖子

{posts.all%} 安托万·德圣埃克苏佩里

{{post.summary}

安托万·德圣埃克苏佩里

{{post.summary}

{%endfor%}
但是,每个独特的帖子现在在一张卡片中显示两次:

任何帮助都将不胜感激!这是我的第一个Django项目,我已经做得很好了。然而,这让我大吃一惊


编辑:我只是想澄清一下,我需要在那张卡片上再贴一张帖子。我想要两张并排展示的卡片,每张都有一个独特的帖子
<div class="container">
    <h2 class="latest-posts">Latest Posts</h2>
    <hr />

    {% for post in posts.all %}

       {% if forloop.counter0|divisibleby:2 %}

       <div class="row">
       {% endif %}

          <div class="col-12 col-md-6">
              <!-- Card -->
              <article class="card animated fadeInRight">
                  <div class="card-block">
                      <h4 class="card-title"><a href="{% url 'post_detail' post.id %}">{{ post.title }}</a></h4>
                      <h6 class="text-muted">Antoine de Saint-Exupéry</h6>
                      <p class="card-text">{{ post.summary }}</p>
                  </div>
                  <div class="card-block text-center">
                      <div class="btn-group hidden-sm-down hidden-md-down" role="group" aria-label="Card buttons">
                          <a href="#" class="card-link">Read more</a>
                      </div>
                  </div>
                  <img class="card-img-bottom img-responsive" style='width:100%; text-align:center;' src="{{ post.image.url }}" alt="White sand" />
              </article><!-- .end Card -->
           </div>


        </div>

    {% if forloop.counter|divisibleby:2 == False %}
    </div>
    {% endif %}

    {% endfor %}

 // When lenght of posts is even the <div> is not close. Close it!</div>
{% if posts.all|length|divisibleby:2 == False %}

    </div>
    {% endif %}

</div><!-- End container -->
最新帖子
{posts.all%} {%if-forloop.counter0 | divisibleby:2%} {%endif%} 安托万·德圣埃克苏佩里

{{post.summary}

{%if-forloop.counter | divisibleby:2==False%} {%endif%} {%endfor%} //当柱子的长度为偶数时,距离就不近了。关上它! {%if posts.all | length | divisibleby:2==False%} {%endif%}
我想在该部门再发一篇帖子。我想要两张卡片并排展示,每张都有一个独特的帖子。这可能吗?我已经修改了答案。希望能有帮助。不幸的是,它仍然以线性方式显示卡片。谢谢你的链接!我将继续我的研究。