I have a json coming from a view in below format in a variable(AllUsers
) :
{
"msg": "data found.",
"user": [
{
"fname": "aaa",
"lname": "aaa",
"add": "add1",
"city": "city1",
},
{
"fname": "aaa2",
"lname": "aaa2",
"add": "add2",
"city": "city2",
}
],
"data_status": "active",
"error": false
}
I need to iterate through this JSON in my template and print in below format.So ideally my loop should run 2 times in this case.
name : aaa
name : aaa2
I tried :
{% for myusers in AllUsers %}
name : {{ user.fname}}
{% end for%}
AND
{%with myusers=AllUsers.user%}
{% for user in myusers %}
name : {{ user.fname}}
{% endfor %}
{% endwith %}
But both of them are not working as loops are not iterating even once.In one of SO threads i read ....You shouldn't "convert it to JSON".... but this is not in my hand...i just get the JSON.
Views
looks like this :
def somefucn(request):
data = {
"msg": "data found.",
"AllUsers": AllUser ## This is where the above JSON resides
"data_status": "active",
"error": false
}
return TemplateResponse(request, 'path/to/Template.html', data)
Where am i going wrong in the iteration? Please help..
the solution was much easier than I thought:
Let's say you have some JSON like POST request with schema like:
"success": true,
"users": [
{
"userId": "76735142",
"username": "user11_01",
"email": "[email protected]",
"isTest": false,
"create_at": "2016-01-29T15:41:16.901Z",
"isBlocked": false
}
(All values in the scheme above were given as example)
And you know that to get this response correctly you need next variables in your post body:
{
"id": "",
"pass_code": "",
"search_for": "",
}
Next you need to send POST request with needed values to the API server:
First you need to install necessary components (from your virtualenv):
pip install requests[security]
[security] flag is needed in order to successful response from HTTPS
VIEWS.PY
import requests
from django.shortcuts import render_to_response
def get_user(request):
args = {}
args.update(csrf(request))
post_data = {'id': '1234', 'pass_code': 'pass', 'search_for': '123456'}
response = requests.post('https://example.com/api/', data=post_data)
args['contents'] = response.json()
return render_to_response('your_templae.html', args)
YOUR_TEMPLATE.HTML
<html>
<head>
<title>testing JSON response</title>
</head>
<body>
<div>
{% for user in contents.users %}
{{ user.userId}}<br>
{{ user.username}}<br>
{% empty %}
Nothing found
{% endfor %}
</div>
</body>
</html>
That's it! Have a nice work! If you mess around you can event fetch headers of this request ;-)
You can use template filter to load json in the template.
Create a file mytags.py as <your-app>/templatetags/mytags.py
The content of mytags.py
as:
import json
from django import template
register = template.Library()
@register.filter
def loadjson(data):
return json.loads(data)
Then in your django .htm/.html template load the tags. eg:
{% load mytags %}
{% for foo in YourJsonData|loadjson %}
{{ foo.something }}
{% endfor %}
Hope this helps.
For more info about advanced templating see: http://www.djangobook.com/en/2.0/chapter09.html#extending-the-template-system
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With