編輯:更新的代碼,即時嘗試從原始結果創建一個新的嵌套字典。 但是字典,目前沒有更新,它唯一的添加/編輯的最後一個值python,django,發送數據到模板
所以我目前的情況下只是有格雷格它並沒有其他人
我當前的代碼如下
# Create your views here.
def index(request):
### Get all the Polices ###
context = {}
for objPolicy in objPolicyData['escalation_policies']:
strPolicyName = objPolicy['name']
if strPolicyName.lower().find('test') == -1:
context['strPolicyName'] = strPolicyName
obj = {}
for objOnCall in objPolicy['on_call']:
obj['strLevel'] = objOnCall['level']
obj['strStartDate'] = getDate(objOnCall['start'])
obj['strStartTime'] = getTime(objOnCall['start'])
obj['strEndDate'] = getDate(objOnCall['end'])
obj['strEndTime'] = getTime(objOnCall['end'])
objUser = objOnCall['user']
obj['strUsername'] = objUser['name']
obj['strUserMobile'] = getUserMobile(objUser['id'])
context['objUsers'] = obj
return render(request, 'oncall/rota.html', context)
採樣數據將是
Network Policy
Level 1: John Smith
Start date: 27 April
Start time: 8am
end Date: 05 May
end time: 8am
Level 2: Bob Smith
Start date: 27 April
Start time: 8am
end Date: 05 May
end time: 8am
Server Policy
Level 1: Jane Doe
Start date: 23 April
Start time: 8am
end Date: 02 May
end time: 8am
Level 2: Greg Brad
Start date: 23 April
Start time: 8am
end Date: 02 May
end time: 8am
and so on...
更新:
@Alix,你現在的解決方案給我下面,我想我需要嵌套列表?作爲2級工程師被髮布,而不是兩次,1級和2級,還缺少政策名稱爲每一個
#!/usr/bin/python
# -*- coding: utf-8 -*-
{'policies': [{
'strStartTime': '09:00AM',
'strEndTime': '09:00AM',
'strLevel': 2,
'strUserMobile': u'',
'strEndDate': 'Monday 02 May',
'strUsername': u'John Smith',
'strStartDate': 'Monday 25 April',
}, {
'strStartTime': '09:00AM',
'strEndTime': '09:00AM',
'strLevel': 2,
'strUserMobile': u''',
'strEndDate': 'Monday 02 May',
'strUsername': u'John Smith',
'strStartDate': 'Monday 25 April',
}, {
'strStartTime': '09:00AM',
'strEndTime': '05:00PM',
'strLevel': 1,
'strUserMobile': u'011151588',
'strEndDate': 'Thursday 28 April',
'strUsername': u'Jane Doe',
'strStartDate': 'Thursday 28 April',
}, {
'strStartTime': '05:00PM',
'strEndTime': '03:30PM',
'strLevel': 1,
'strUserMobile': 'User does not have a company phone no',
'strEndDate': 'Thursday 28 April',
'strUsername': u'Fred Perry',
'strStartDate': 'Wednesday 27 April',
}, {
'strStartTime': '09:00AM',
'strEndTime': '07:00AM',
'strLevel': 1,
'strUserMobile': 'User does not have a company phone no',
'strEndDate': 'Tuesday 03 May',
'strUsername': u'Sally Cinomon',
'strStartDate': 'Monday 25 April',
}]}
只是在呈現調用中發送它。像 'return render(request,「oncall/rota.html」,{「policies」:objPolicyData})' – alix
@alix爲什麼不放在ans? –
認爲這很簡單。我應該那樣做嗎? @RajaSimon – alix