带for循环的网格线

时间:2019-05-23 17:15:34

标签: django twitter-bootstrap jinja2

所以我想要三条网格线,例如<div class="col-6 col-md-4"> 每条网格线当然都有内容。因此代码可能会告诉我我要实现的目标

<div class="row">
  <div class="col-6 col-md-4">

    {% for all_episode in episode %}
    <div class="card">
      <a href="{% url 'episode_detail' slug=all_episode.slug %}">
        <img class="card-img-top" src='{{all_episode.image.url}}'></a>

      <div class="card-body">
        <h5 class="card-title">
          <a href="{% url 'episode_detail' slug=all_episode.slug %}">{{ all_episode.title }}</a>
        </h5>
        <p class="card-text">{{ all_episode.story |slice:":100" }}...</p>
      </div>

      <div class="card-footer">
        <small class="text-muted">
          <span class="h5">
            <a href="{% url 'series_detail' slug=all_episode.series.slug %}">{{ all_episode.series }}</a>
          </span> /
          <span class="h6">
            <a href="{% url 'season_detail' slug=all_episode.season.slug %}">{{ all_episode.season }}</a>
          </span>
        </small>
      </div>
      {% endfor %}

    </div>
  </div>
</div>

但是使用上面的代码,我使卡垂直对齐。因此,一张卡占据了整行,而应该有三张卡。

1 个答案:

答案 0 :(得分:1)

请小心您的缩进。很难阅读代码。 您可以在col div中添加card类,并且需要将循环放入row div中,但要在col div之外。

<div class="row">
  {% for all_episode in episode %}
    <div class="col-6 col-md-4 card">
      <a href="{% url 'episode_detail' slug=all_episode.slug %}">
        <img class="card-img-top" src='{{all_episode.image.url}}'>
      </a>
      <div class="card-body">
        <h5 class="card-title">
          <a href="{% url 'episode_detail' slug=all_episode.slug %}">{{ all_episode.title }}</a>
        </h5>
        <p class="card-text">{{ all_episode.story |slice:":100" }}...</p>
      </div>

      <div class="card-footer">
        <small class="text-muted">
          <span class="h5">
            <a href="{% url 'series_detail' slug=all_episode.series.slug %}">{{ all_episode.series }}</a>
          </span> / 
          <span class="h6">
            <a href="{% url 'season_detail' slug=all_episode.season.slug %}">{{ all_episode.season }}
            </a>
          </span>
        </small>
      </div>
    </div>
  {% endfor %}
</div>