gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:30:40 24 4
gpt4 key购买 nike

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

最佳答案

  • 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.



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

    关于django - Django:HttpResponse与HttpResponseRedirect与render_to_response有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1921523/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com