Python 将多个词典发送到django中的模板

Python 将多个词典发送到django中的模板,python,html,django,django-templates,django-views,Python,Html,Django,Django Templates,Django Views,所以,我对django和模板制作有点陌生,我不知道该怎么做。在我的项目结构中有一个名为services.py的文件,其中有几个函数执行身份验证、调用API、返回数据、解析数据,并将它们输入到多个字典中。API描述如下: { CheckName: "AppPools", Description: "DefaultAppPool", GroupName: "Server1", Links: [ { description: "Re

所以,我对django和模板制作有点陌生,我不知道该怎么做。在我的项目结构中有一个名为services.py的文件,其中有几个函数执行身份验证、调用API、返回数据、解析数据,并将它们输入到多个字典中。API描述如下:

{
CheckName: "AppPools",
Description: "DefaultAppPool",
GroupName: "Server1",
Links: [
{
description: "Recycles the DefaultAppPool app pool.",
link: "Recyle/Server1/DefaultAppPool",
title: "Recycle"
},
{
description: "Stops the DefaultAppPool app pool.",
link: "Stop/Server1/DefaultAppPool",
title: "Stop"
},
{
description: "Starts the DefaultAppPool app pool.",
link: "Start/Server1/DefaultAppPool",
title: "Start"
}
]
},
{
CheckName: "AppPools",
Description: "FinancialServices",
GroupName: "ST0PWEB12",
Links: [
{
description: "Recycles the FinancialServices app pool.",
link: "Recyle/Server2/FinancialServices",
title: "Recycle"
},
{
description: "Stops the FinancialServices app pool.",
link: "Stop/Server2/FinancialServices",
title: "Stop"
},
{
description: "Starts the FinancialServices app pool.",
link: "Start/Server2/FinancialServices",
title: "Start"
}
]
},
这里有一个层次结构:

CheckName1
     GroupName1
         Description1
         Description2
     GroupName2
         Description3
         Description4
CheckName2
     GroupName1
         Description1
         Description2
     GroupName2
         Description3
         Description4
我已使用以下格式将数据存储在字典中:

{CheckName1:(GroupName1,GroupName2, GroupName3), CheckName2:(GroupName4,Grouonam5, GroupName6)}
{Groupname1:(Description1, Description2, Description3), GroupName2:(Description5. Description6, Description7)}
名为groupsInChecks的函数创建具有以下格式的dict:

{CheckName1:(GroupName1,GroupName2, GroupName3), CheckName2:(GroupName4,Grouonam5, GroupName6)}
{Groupname1:(Description1, Description2, Description3), GroupName2:(Description5. Description6, Description7)}
名为serviesInGroups的函数创建具有以下格式的dict:

{CheckName1:(GroupName1,GroupName2, GroupName3), CheckName2:(GroupName4,Grouonam5, GroupName6)}
{Groupname1:(Description1, Description2, Description3), GroupName2:(Description5. Description6, Description7)}
然后它们返回DICT,DICT嵌套为值

我在视图中有自己的视图。py

def app_status(request):

    data=ntlmAuthGetRequest() #does api request gets json data
    groups=groupsInChecks(data) #parses into first dict
    services=serviesInGroups(data) #parses into second dict
    return render(request, 'application_status/app_status_page.html') #this is where I'm lost, how do I pass this to the html file!
此外,在html文件中,我需要使用该数据动态创建一个表。我也不知道该怎么做


欢迎提供任何提示。另外,如何将我的services.py导入到视图中以供使用。

不管您有多少DICT或其他值。无论数字是多少,它们都会在第三个参数中传递给
render
,这本身就是一个dict。

不管您有多少dict或其他值。无论数字是多少,它们都会在第三个参数中传递到
render
,这本身就是一个命令。

在views.py文件中,您必须导入services.py文件才能使用它提供的功能

import services

def app_status(request):
    data=ntlmAuthGetRequest() #does api request gets json data
    groups=groupsInChecks(data) #parses into first dict
    services=serviesInGroups(data) #parses into second dict
    return render(request, 'application_status/app_status_page.html', context={'groups':groups,'services':services}) 
在html模板文件中,可以通过以下方式访问上下文变量:

<p>{{ groups }}</p>
<p>{{ services }}</p>
{{groups}

{{services}}

在views.py文件中,必须导入services.py文件才能使用它提供的功能

import services

def app_status(request):
    data=ntlmAuthGetRequest() #does api request gets json data
    groups=groupsInChecks(data) #parses into first dict
    services=serviesInGroups(data) #parses into second dict
    return render(request, 'application_status/app_status_page.html', context={'groups':groups,'services':services}) 
在html模板文件中,可以通过以下方式访问上下文变量:

<p>{{ groups }}</p>
<p>{{ services }}</p>
{{groups}

{{services}}


将多个变量传递给django模板


将多个变量传递给django模板


如果services.py文件在同一目录下,您可以通过views.py文件中的
导入服务
导入services.py文件。如果services.py文件在同一目录下,您可以通过views.py文件中的
导入服务
导入services.py文件。谢谢我的伙计,我会尝试一下,看看它是否有效!一旦我在hrml的变量中有了它,iloop和reference items如何设置dict@Rajanif groups值中的项检查是服务文件use services=service.serviceInGroups(data)HTML*中提供的函数,而不是hrml。例如,如果我有一个dict{CheckName1:(GroupName1,GroupName2,GroupName3),CheckName2:(GroupName4,groupname5,GroupName6)}在HTML中引用GroupName4,就像你想引用GroupName4一样,它将是{{CheckName2.0}谢谢我的人,我会尝试一下,看看它是否有效!一旦我在hrml的变量中有了它,iloop和reference items如何设置dict@Rajanif groups值中的项检查是服务文件use services=service.serviceInGroups(data)HTML*中提供的函数,而不是hrml。例如,如果我有一个dict{CheckName1:(GroupName1,GroupName2,GroupName3),CheckName2:(GroupName4,groupname5,GroupName6)}引用HTMLIKE中的GroupName4,那么我该怎么做呢?如果你想引用GroupName4,它将是{{CheckName2.0}