2016-04-27 92 views
0

編輯:更新的代碼,即時嘗試從原始結果創建一個新的嵌套字典。 但是字典,目前沒有更新,它唯一的添加/編輯的最後一個值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', 
    }]} 
+0

只是在呈現調用中發送它。像 'return render(request,「oncall/rota.html」,{「policies」:objPolicyData})' – alix

+1

@alix爲什麼不放在ans? –

+0

認爲這很簡單。我應該那樣做嗎? @RajaSimon – alix

回答

1

與如何使用數據模板只是擴大我的上述評論

return render(request, "oncall/rota.html", {"policies": objPolicyData['escalation_policies']) 

然後,在你的模板文件,你可以做像這樣:

{% for policy in policies %} 
    {% for objOnCall in policy.on_call %} 
     <p> Level: {{ objOnCall.level }} </p> 
     <p> Start Time: {{ objOnCall.start }} </p> 
    {% endfor %} 
{% endfor %} 

UPDATE

根據您最近更新的問題;

你說,

however the dictionary is currently not updating, its only adding/editing the last value

這是正確的,因爲你沒有一個數組包含了你的策略對象做。您只將循環中的最後一個值設置爲字典。這就是爲什麼你只能得到最後一個對象。

這應該做的工作;

# Create your views here. 
def index(request): 
    ### Get all the Polices ### 
    policies = [] 
    for objPolicy in objPolicyData['escalation_policies']: 
     strPolicyName = objPolicy['name'] 
     policy = {} 
     policy['name'] = strPolicyName 
     if strPolicyName.lower().find('test') == -1: 
      policy = {} 
      policy['strPolicyName'] = strPolicyName # add policy name here 
      policy['objUsers'] = [] # define an empty array for users 
      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']) 
       policy['objUsers'].append(obj) # add each user to the users array belongs to this policy object 

     policies.append(policy) # and finally append final and prepared policy object to our main policies array. 

    context = {"policies": policies} 
    return render(request, 'oncall/rota.html', context) 

現在,您可以對模板中的for loop內的此數組執行任何操作。 (請參閱我上面的示例)

+0

如上所述,在將模板發送到模板之前預格式化和減少數據不明智嗎?在發送數據之前,我有一些運行格式化的功能 – AlexW

+0

@AlexW但是我不知道您需要多少和哪些部分的數據。我剛纔展示瞭如何傳遞字典並在模板中使用它們。在我的例子中,我發送了'objPolicyData ['escalation_policies']'。你可以發送任何你想要的東西。如果字典有效,則不必預先格式化它。只需在模板中使用它們即可。 Django允許這樣做。 – alix

+0

我想我問如何從我的字符串創建一個新的詞典,如果這是有道理的? – AlexW

0

我認爲,這個問題並不好。

有很多種解決方案爲您的目標。 甚至,Django的文件。

這只是示例。

您可以在render發送數據:

 context = dict() 
     for objOnCall in objPolicy['on_call']: 
      obj = dict() 
      obj['strLevel'] = objOnCall['level'] 
      obj['strStartDate'] = getDate(objOnCall['start']) 
      obj['strStartTime'] = getTime(objOnCall['start']) 
      obj['strEndDate'] = getDate(objOnCall['end']) 
      obj['strEndTime'] = getTime(objOnCall['end']) 
      obj['objUser'] = objOnCall['user'] 
      obj['strUsername'] = objUser['name'] 
      obj['strUserMobile'] = getUserMobile(objUser['id']) 
      context[objUser['name']] = obj 
return render(request, 'oncall/rota.html', context) 
+0

對不起,我是django和python的新手,所以不知道如何完全問我在做什麼即時通訊,這段代碼給了一個錯誤上下文[objUser ['name'] = obj ^ SyntaxError:invalid syntax – AlexW

+0

sorry ,我有一個拼寫錯誤。 ''[objUser ['name'] = obj''意味着'context [objUser ['name']] = obj' – beCurious

+0

網絡策略在測試時有兩次bob smith,我認爲第二個寫過第一個,如果你明白我的意思? – AlexW