如何在Django视图中使用for循环返回每个迭代

2024-04-27 13:39:00 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在模板中打印所有for循环迭代

型号

This is my model

视图

enter image description here


Tags: 视图模板for型号
1条回答
网友
1楼 · 发布于 2024-04-27 13:39:00

不要使用id,Django非常擅长让您编写简单易读的代码。假设Client_Process有一个ForeignKeyClient(即一个Client可以有多个Client_Process

在你看来:

def view_client(request):
    clients = Client.objects.all().prefetch_related('client_process_set')
    return render(request, 'client_list.html', {'clients': clients})

在你的客户身上_列表.html地址:

{% for client in clients %}
   {{ client.name }}
   Client processes:
   {% for process in client.client_process_set.all %}
        {{ process.name }}
   {% endfor %}
{% endfor %}

相关问题 更多 >