嵌套的for循环如何打印django模板中的数据?

oknrviil  于 2023-01-03  发布在  Go
关注(0)|答案(1)|浏览(138)

我尝试使用嵌套循环在模板中显示一个用户的追随者和一些他们的追随者和追随者,但是显示的信息不正确。当我在终端中打印查询集时,它显示了正确的信息。由于我以前没有在模板中使用过嵌套循环,我希望得到如何正确显示此信息的指导。
view.py :

def get_context_data(self, *args, **kwargs):
        context = super(FollowerView,self).get_context_data(*args, **kwargs)
        user = context['user']
        myfollowers = user.is_following.all()
        for target_list in myfollowers:
            user_obj = User.objects.get(username=target_list)
            followers_obj = user_obj.is_following.all()
            followerings_obj = user_obj.userprofile.follower.all()
            print(followers_obj,user_obj) #this return two query sets. which are given below
        context['myfollowers_data']= followers_obj
        context['myfollowerings_data']= followerings_obj
        return context

终端输出:

Quit the server with CTRL-BREAK.
<QuerySet [<UserProfile: admin>]> flash 
<QuerySet [<UserProfile: admin>, <UserProfile: flash>]> maddy

这是正确的信息。问题是模板。
遵循模板:

{% for follower in user.is_following.all %}
                <div class="followers-body">
                 <img class="img-responsive img-circle" src="{{follower.avatar.url}}" alt="">
                 <div class="name-box">
                 
                  <h4>{{follower}}</h4>
                  <span>@{{follower}}</span>
                  <div class="followers-base">
                   <ul class="list-group">
                    <li class="list-group-item full-width">
                     <div class="pull-left" style="margin-right: 20px;">
                     {% if myfollowers_data %}
                     {% for user in myfollowers_data %}
                      <img src="{{user.avatar.url}}" alt="" class="img-circle max-w-40 ">
                      {% endfor %}
                      {% endif %}
                      <span> {{myfollowers_data.count}} Followers </span>#this returning 2,it suppose to return 1 as flash(user object) has one follower
                     </div><!--/ pull-left -->
                    </li> 
                    <li class="list-group-item full-width">
                     <div class="pull-left">
                       {% for user in myfollowerings_data %}
                      <img src="{{user.avatar.url}}" alt="" class="img-circle max-w-40 ">
                      {% endfor %}
                      <span> {{myfollowerings_data.count}} Following </span>
                     </div><!--/ pull-right -->
                    </li><!--/ list-group-item -->                     
                   </ul>
                  </div><!--/ followers-base -->
                 </div><!--/ name-box -->
                 <span><a href="#" class="kafe-btn kafe-btn-mint-small"> Follow</a></span>
                </div><!--/ followers-body -->
                {% endfor %}

如果需要更多细节而不是在评论会上告诉我,我会用这些信息更新我问题。谢谢。

flvlnr44

flvlnr441#

在内部循环中使用不同的变量:

...
        <li class="list-group-item full-width">
            <div class="pull-left">
                {% for usr in myfollowerings_data %}
                    <img src="{{usr.avatar.url}}" alt="" class="img-circle max-w-40 ">
                {% endfor %}
                 <span> {{myfollowerings_data.count}} Following </span>
             </div><!--/ pull-right -->
        </li><!--/ list-group-item -->
...

目标变量具有相同名称的嵌套循环实际上共享一个变量

相关问题