Django:HttpResponse与HttpResponseRedirect与render_to_response有什么区别

Django: What is the difference b/w HttpResponse vs HttpResponseRedirect vs render_to_response

上面提到的东西给了我几乎相同的结果,想知道它们之间的主要区别是什么。


  • response = HttpResponse("Here's the text of the Web page.")
    将使用HTTP代码200(OK)创建一个新的HttpResponse对象,并将内容传递给构造函数。通常,只应将其用于很小的响应(例如AJAX表单返回值,如果它真的很简单-仅一个数字)。

  • HttpResponseRedirect("http://example.com/")
    将使用HTTP代码302(临时找到/移动)创建一个新的HttpResponse对象。仅应将其用于重定向到另一页(例如,在成功执行POST之后)

  • 从文档中:

    class HttpResponseRedirect
    The constructor takes a single argument -- the path to redirect to.
    This can be a fully qualified URL
    (e.g. 'http://www.yahoo.com/search/')
    or an absolute URL with no domain
    (e.g. '/search/'). Note that this
    returns an HTTP status code 302.

    足够说...

    render_to_response(template[, dictionary][, context_instance][,mimetype])
    Renders a given template with a given context dictionary and returns
    an HttpResponse object with that
    rendered text.

    是调用以使用给定的变量字典呈现模板来为您创建响应的调用。这是您大多数时候应该使用的方式,因为您希望将表示逻辑保留在模板中,而不是在代码中。