0

EDIT: Updated code, im trying to create a new nested dictionary from the orginal results. however the dictionary is currently not updating, its only adding/editing the last value

so my current context just has greg on it and no one else

my current code is as below

# 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)

sample data would be

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...    

Update:

@Alix, your current solution gives me the below, i think i need nested lists? as the level 2 engineer gets posted twice instead of level 1 and level 2, also missing the policy names for each one

#!/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',
    }]}
6
  • just send it within render call. like return render(request, "oncall/rota.html", {"policies": objPolicyData}) Commented Apr 27, 2016 at 14:49
  • 1
    @alix why not placed in ans ? Commented Apr 27, 2016 at 14:51
  • thought it was simple. should i do that? @RajaSimon Commented Apr 27, 2016 at 14:52
  • i thought it would be best to clean the data up before sending it to a template? as you can see i am running some other functions on cleaning the data first Commented Apr 27, 2016 at 15:06
  • @alix i have updated the code and question to hopefully make more sense Commented Apr 27, 2016 at 15:53

2 Answers 2

1

Just expanding my comment above with how to use the data in template:

You can send your data within render:

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

Then, in your template file, you can do something like this:

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

UPDATE

According to the your last update to the question;

You said,

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

This is right, because you don't have an array contains your policy objects. You only set the last value in the loop to the dictionary. This is why you are getting only last object.

This should do the work;

# 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)

Now you can do anything you want with this array inside a for loop in template. (see my above example)

Sign up to request clarification or add additional context in comments.

5 Comments

as above, is it not wise to pre format and reduce data before sending it to template? i have some functions i run that do formatting and such too before sending the data
@AlexW But i do not know how much and which parts of your data will be needed to you. i just showed how you can pass dictionaries and use them in templates. in my example, i sent objPolicyData['escalation_policies']. you can send anything you want. if dictionary is valid, you don't have to pre-format it. just use them as they are in template. Django allows that.
i think im asking how to create a new dictionary from my strings, if that makes sense?
i got context undefined so added context = {} to the top, im now getting not getting Network Policy, server policy, i only get policies, then udner that i get user info, but also user level is repeated twice, ill update the cdoe to show what my result is
@AlexW updated the view. removed pre defined context. just copy and paste this array. just try this and let me know if it works.
0

I think, this question is not good.

there are many kind of solution for your goal. even, django documents.

this is just sample.

        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)

4 Comments

sorry, i am new to django and python so dont quite know how to fully ask for what im trying to do yet, this code give an error context[objUser['name'] = obj ^ SyntaxError: invalid syntax
sorry, i have a misspel. context[objUser['name'] = obj means context[objUser['name']] = obj
the network policy has level 2 bob smith in twice when tested, i think the second one over writes the first, if you know what i mean?
i have updated my code to show what i currently have and the issues behind it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.