【发布时间】:2016-03-22 03:13:14
【问题描述】:
这是我得到的 json 输出 -
{
"count": 2,
"next": null,
"previous": null,
"check": [
{
"url": "http://127.0.0.1:8000/app_name/cities/1/persons/?format=json",
"id": 1,
"name": "City1"
},
{
"url": "http://127.0.0.1:8000/app_name/cities/2/persons/?format=json",
"id": 2,
"name": "City2"
}
]
}
我正在使用下面给出的视图-
def get_name(request):
data = requests.get('http://127.0.0.1:8000/app_name/cities/?format=json')
context = RequestContext(request, {
'cities': data.check,
})
return render_to_response('template', context)
为了能够像这样在我的模板中使用这些数据 -
template.html
{% block names %}
{% for city in cities %}
<a href="{% url 'next_view_name' city.id %}"><p>{{city.name}}</p></a>
{% endfor %}
{% endblock %}
但是,这给了我以下错误 -
'Response' object has no attribute 'check'
json数据返回字典值的正确流程是什么?
【问题讨论】:
标签: python json django django-templates django-rest-framework