【发布时间】:2012-03-25 15:32:29
【问题描述】:
如果我启动开发服务器并转到某个视图。我没有得到回应。
我的看法是这样的
from django.http import HttpResponse, Http404
from goals.models import Child
import json
def viewChildren(request):
jsonData= []
allChildren = Child.objects.all()
for child in allChildren:
jsonData.append({'id': child.id, 'name': child.name})
return HttpResponse(json.dumps(jsonData),mimetype="application/json")
为了测试发生了什么,我做了以下操作
python manage.py shell
>>> import goals.views as v
>>> r = v.viewChildren('')
>>> print r
Content-Type: application/json
[{"id": 1, "name": "Test Child"}, {"id": 2, "name": "Second child"}]
所以我知道视图是正确的,并且在调用视图后正在传递正确的数据...但是当我从浏览器查看时,这并没有被传递。
有什么建议吗? (这类似于很多问题,但我发现没有一个显示上述情况,即视图提供了正确的响应,但从浏览器却没有)
【问题讨论】:
-
我还在 urls.py 中添加了正确的 url,其中包含以下内容:
url(r'children/$', 'goals.views.viewChildren')我正在尝试访问http://localhost:8080/children/的页面,管理页面工作正常
标签: python django json httpresponse