为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的每个个人资料都返回自己的行而不是 3 行?

为什么我的每个个人资料都返回自己的行而不是 3 行?

江户川乱折腾 2022-11-29 16:55:58
所以,我有一个循环遍历我的用户配置文件并随机显示其中的一些。我应该为每一行返回 3 个配置文件,但我让每个配置文件都有自己的行。我在这里做错了什么>{% for profile in random_profiles %}  <div class="row">    <div class="col-md-4">      <div class="card mb-4 shadow-sm">        <img src="{{  profile.photo.url  }}"  width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false"  rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>        <div class="card-body">          <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>          <div class="d-flex justify-content-between align-items-center">            <div class="btn-group">              <button type="button" class="btn btn-sm btn-outline-secondary">View</button>              <button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>            </div>            <small class="text-muted">9 mins</small>          </div>        </div>      </div>    </div>  </div></div>{% endfor %}
查看完整描述

1 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

您正在为每个配置文件创建一行。标记在 for 循环内。我想要么你必须使用 2 个循环,一个用于配置文件,另一个用于每行中的配置文件数量,或者保持计数,如果计数达到 4,则创建额外的行,然后重置计数。实现第二种方法的方法之一可以是 -


{% for profile in random_profiles %}

  {% if forloop.first or forloop.counter0 % 4 == 0 %} 

    <div class="row">

  {% endif %}

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

          <div class="card mb-4 shadow-sm">

            <img src="{{  profile.photo.url  }}"  width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false"  rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>

            <div class="card-body">

              <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>

              <div class="d-flex justify-content-between align-items-center">

                <div class="btn-group">

                  <button type="button" class="btn btn-sm btn-outline-secondary">View</button>

                  <button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>

                </div>

                <small class="text-muted">9 mins</small>

              </div>

            </div>

          </div>

        </div>


      </div>

  {% if forloop.first or forloop.counter0 % 4 == 0 %} 

    </div>

  {% endif %}

{% endfor %}


查看完整回答
反对 回复 2022-11-29
  • 1 回答
  • 0 关注
  • 151 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号