【发布时间】:2016-03-30 22:45:06
【问题描述】:
我想在我的 API 中使用我使用 django_rest_framework 创建的 api。我想使用在我的视图和模板中生成的 json。这是我的 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
]
},
]
我想像这样在我的模板中使用它 -
{% for c in club %}
{{c.url}}{{c.club_name}}
{% endfor %}
我尝试了以下视图,分别获取 unicode 字符串,但 url 和 club_name 将在不同的上下文中 -
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)
还有其他方法可以做我想做的事吗?
【问题讨论】:
标签: python json django django-templates django-views