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)
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)
- 'ColorField' object has no attribute 'model' django
- Django URL and Views
- For upgrade Django 1.9.7 to 1.9.9 "webpackJsonp is not defined" appears in admin page menu
- Update view not working? Django
- Django-taggit tags with whitespace in name?