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'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',
    }]}
阿利克斯

只是在上面扩展我的评论,了解如何使用模板中的数据:

您可以在以下范围内发送数据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 loopin模板中的此数组执行任何操作(请参阅我上面的示例)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章