python,Django,将数据发送到temp

2024-04-20 11:04:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

所以我现在的环境里只有格雷格,没有其他人

我现在的代码如下

# 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',
    }]}

Tags: objtimestartmayendmondayaprilstrlevel
2条回答

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

您可以在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 %}

更新

根据你上次更新的问题

你说过

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

我认为,这个问题不好。你知道吗

对于你的目标有很多种解决方案。 甚至,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)

相关问题 更多 >