Reputation: 859
I want to use the api I created using django_rest_framework
in my API.I want to use the json generated in my views and templates. This is my json -
[
{
"url": "http://127.0.0.1:8000/app/clubs/1/persons/",
"id": 1,
"club_name": "club1",
"persons": [
1,
2
]
},
{
"url": "http://127.0.0.1:8000/app/clubs/2/persons/",
"id": 2,
"club_name": "club2",
"persons": [
1,
4
]
},
]
and I want to use it in my templates like so -
{% for c in club %}
{{c.url}}{{c.club_name}}
{% endfor %}
I tried the following view,getting unicode strings out separately but then url
and club_name
would be in different contexts -
def clubs(request):
data = requests.get('http://127.0.0.1:8000/app/clubs/').json()
count=0
for i in data:
count+=1
g=[]
identity=[]
for j in range(count):
g.append(data[j]['club_name'])
identity.append(data[j]['url'])
context = RequestContext(request, {
'club_name': g,'count':count,'url':identity,
})
return render_to_response('imgui/clubs.html', context)
Is there any other way to do what I want?
Upvotes: 0
Views: 70
Reputation: 17751
The club
you need in your template is what you call data
in your view:
def clubs(request):
data = requests.get('http://127.0.0.1:8000/app/clubs/').json()
context = RequestContext(request, {
'club': data,
})
return render_to_response('imgui/genres.html', context)
Not relevant to the problem itself, but a few hints:
This loop:
count=0
for i in data:
count+=1
is equivalent to: count = len(data)
This loop:
for j in range(count):
g.append(data[j]['club_name'])
identity.append(data[j]['url'])
can be better written as:
for c in data:
g.append(c['club_name'])
identity.append(c['url'])
Upvotes: 1