python,django,将数据发送到模板

时间:2016-04-27 14:40:58

标签: python django

编辑:更新了代码,我试图从原始结果创建一个新的嵌套字典。 但是字典当前没有更新,它只添加/编辑最后一个值

所以我目前的背景只有greg而没有其他人

我目前的代码如下

# 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'01234 5678',
    'strEndDate': 'Monday 02 May',
    'strUsername': u'John Smith',
    'strStartDate': 'Monday 25 April',
    }, {
    'strStartTime': '09:00AM',
    'strEndTime': '09:00AM',
    'strLevel': 2,
    'strUserMobile': u'01234 5678'',
    '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',
    }]}

2 个答案:

答案 0 :(得分:1)

只需扩展我在上面的注释,了解如何使用模板中的数据:

您可以在render内发送数据:

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 %}

<强>更新

根据您对该问题的上次更新;

你说,

  

然而,字典目前还没有更新,它只是   添加/编辑最后一个值

这是对的,因为您没有包含策略对象的数组。您只需将循环中的最后一个值设置为字典。这就是为什么你只得到最后一个对象。

这应该做的工作;

# 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内使用此数组执行任何操作。 (见上面的例子)

答案 1 :(得分:0)

我认为,这个问题并不好。

您的目标有很多种解决方案。 甚至,django文件。

这只是样本。

        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)