所以我对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
IVe使用以下格式将数据存储在词典中
名为groupsInChecks的函数使用以下格式创建一个dict:
{CheckName1:(GroupName1,GroupName2, GroupName3), CheckName2:(GroupName4,Grouonam5, GroupName6)}
名为serviesInGroups的函数使用以下格式创建一个dict:
{Groupname1:(Description1, Description2, Description3), GroupName2:(Description5. Description6, Description7)}
然后,他们返回已嵌套为值的dicts。
我在views.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 im lost how do i pass this to the html file
此外,在html文件中,我需要动态创建一个包含该数据的表,而不确定如何操作。
任何提示都会很棒。另外,我如何将我的services.py导入到我的视图中以供使用。
谢谢!
答案 0 :(得分:2)
在 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>
答案 1 :(得分:1)
您拥有多少个词汇或其他值并不重要。无论数字是多少,它们都会在第三个参数中传递给render
,这本身就是一个字典。
答案 2 :(得分:-1)
将多个变量传递给django模板
from django.shortcuts import render_to_response
from django.template import RequestContext
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_to_response('application_status/app_status_page.html',
{'data': data, 'groups': groups,'services':services},
context_instance=RequestContext(request))
from django.shortcuts import render_to_response
from django.template import RequestContext
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_to_response('application_status/app_status_page.html',
{'data': data, 'groups': groups,'services':services},
context_instance=RequestContext(request))