必承其重 | 欲带皇冠 2010-06-08 18:18 采纳率: 50%
浏览 30

django:ajax视图结构

I want to know the correct way to structure ajax views in django.

say i do something like :

def foo_json(request):
    if is_ajax():
        # return JSON here

and make it available as a resource at something like '/foo/data/'..

all is fine..

but if I point the browser at '/foo/data/' .. obviously I get an error (debug) like: app.views.foo_json didn't return an HttpResponse object.

so... my question is:

Whats the best way structure this kind of view?

..should I return an HTTP response code ..maybe 404 / 405 ... or something else? - not sure of the best way to handle this, any advice appreciated :)

EDIT: ..the use-case that triggered this question was ajax PROXY view :

def foo_proxy(request):
    if is_ajax():
        # proxy external URL
        # return resource
    else: 
        # ? do what with browser request
  • 写回答

2条回答 默认 最新

  • weixin_33681778 2010-06-08 18:37
    关注

    If you don't know what you want in the else clause, are you sure you need the if? Why test for is_ajax at all? Why not just return JSON data to the browser?

    评论

报告相同问题?