Django-模板未显示我传递的整个上下文列表

时间:2018-08-30 18:53:20

标签: django django-models django-templates django-views

这让我发疯。我在外壳程序中运行了逻辑,我要填充的列表保存了应该传递给模板的所有值,但是在呈现模板时,它仅显示“ each_role”迭代的第一遍

该外壳显示了8个放置在列表中的对象,而我所做的只是传递要在模板中呈现的对象。我在两个测试用例中都使用的用户是我的管理员用户,该用户被分配了“财务”和“ IT”角色,因此为什么它显示所有8个对象。前5个对象是“ IT”对象,其余3个是“财务”对象。

这种行为的任何原因以及为什么它不会显示网页上的所有行? 我附加了实际页面的图像。谢谢! WebPage image

Shell

>>> for each_role in all_roles:
...     if each_role in user_roles:
...             list_of_reqs = RequisitionLine.objects.filter(assistance=each_role.id)
...             for line in list_of_reqs:
...                     parent = line.parent_req
...                     if parent in req_header_list:
...                             continue
...                     else:
...                             req_header_list.append(parent)
...             for i in range(len(req_header_list)):
...                     hold_line_count = RequisitionLine.objects.filter(Q(parent_req=req_header_list[i]) & Q(assistance=each_role.id))
...                     for j in hold_line_count:
...                             req_header_line_list.append(j)
...                             print(j)
...
RequisitionLine object (30)
RequisitionLine object (32)
RequisitionLine object (33)
RequisitionLine object (34)
RequisitionLine object (39)
RequisitionLine object (29)
RequisitionLine object (37)
RequisitionLine object (38)
>>>
>>> req_header_line_list
[<RequisitionLine: RequisitionLine object (30)>, <RequisitionLine: RequisitionLine object (32)>, <RequisitionLine: RequisitionLine object (33)>, <RequisitionLine: RequisitionLine object (34)>, <RequisitionLine: RequisitionLine object (39)>, <RequisitionLine: RequisitionLine object (29)>, <RequisitionLine: RequisitionLine object (37)>, <RequisitionLine: RequisitionLine object (38)>]
>>>

Views.py

def assistance_list(request):

    req_header_list = []
    req_header_line_list = []

    user = CustomUser.objects.get(username=request.user)
    all_roles = UserRoles.objects.all() 
    user_roles = user.roles.all()

    for each_role in all_roles:
        if each_role in user_roles:
        # for u_role in user_roles:
            # if each_role == u_role:
            list_of_reqs = RequisitionLine.objects.filter(assistance=each_role.id)
            for line in list_of_reqs:
                parent = line.parent_req
                if parent in req_header_list:
                    continue 
                else:
                    req_header_list.append(parent)

            for i in range(len(req_header_list)):                                                    
                hold_line_count = RequisitionLine.objects.filter(Q(parent_req=req_header_list[i]) & Q(assistance=each_role.id))

                for j in hold_line_count:
                    req_header_line_list.append(j)

        return render(request, 'req/assist_req_list.html', {'req_header_line_list':req_header_line_list})

assist_req_list.html

<h1>Assist Requisition</h1>

    <table>
            {% for requisition in req_header_line_list %}    
        <tr>
            <td>
                <a href="{% url 'requisition:assist_req_seq' requisition.parent_req.id requisition.sequence %}">{{ requisition }}</a>     
            </td>
        </tr>
        {% endfor %}
    </table>    

1 个答案:

答案 0 :(得分:1)

您有缩进错误;您的return位于外部for循环内,因此将在第一次迭代后触发。