Django:有什么区别b / w HttpResponse vs HttpResponseRedirect vs render_to_response [英] Django: What is the difference b/w HttpResponse vs HttpResponseRedirect vs render_to_response

查看:220
本文介绍了Django:有什么区别b / w HttpResponse vs HttpResponseRedirect vs render_to_response的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The above mentioned things are giving me almost the same results was wondering whats the main difference in them.

推荐答案


  1. response = HttpResponse(这是网页的文本。)

    将创建一个新的 HttpResponse 对象与HTTP代码200(确定),并将内容传递给构造函数。一般来说,你应该只使用这个真正的小的响应(如AJAX表单返回值,如果它真的很简单 - 只是一个数字)。

  1. response = HttpResponse("Here's the text of the Web page."):
    will create a new HttpResponse object with HTTP code 200 (OK), and the content passed to the constructor. In general, you should only use this for really small responses (like an AJAX form return value, if its really simple - just a number or so).

HttpResponseRedirect(http://example.com/)

将使用HTTP代码302(找到/暂时移动)创建一个新的 HttpResponse 对象。 此应该仅用于重定向到另一个页面(例如POST成功后)

从文档中:


class HttpResponseRedirect
构造函数需要一个单个参数 - 重定向到的路径。
这可以是一个完全限定的URL
(例如' http://www.yahoo。 com / search / ')
或没有域
的绝对URL(例如'/ search /')。请注意,这个
返回HTTP状态代码302.

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])

使用给定的上下文字典渲染给定的模板,并返回
一个具有
呈现文本的HttpResponse对象。

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.

是使用给定的变量字典渲染模板的一个调用来为您创建响应。 这是您应该使用大部分时间,因为您希望将演示逻辑保留在模板中,而不是代码中。

is a call to render a template with given dictionary of variables to create the response for you. This is what you should be using most of the time, because you want to keep your presentation logic in templates and not in code.

这篇关于Django:有什么区别b / w HttpResponse vs HttpResponseRedirect vs render_to_response的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆