HttpResponse与HttpResponseRedirect与render_to_response有什么区别? [英] What is the difference between HttpResponse vs HttpResponseRedirect vs render_to_response?

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

问题描述

上述情况给我几乎相同的结果.我想知道它们之间的主要区别是什么.

The above-mentioned things give me almost the same results. I was wondering what is the main difference in them.

推荐答案

  1. response = HttpResponse(这是网页的文本."):
    将使用HTTP代码200(OK)创建一个新的 HttpResponse 对象,并将其内容传递给构造函数.通常,仅应将其用于很小的响应(例如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之后)

从文档中:

类HttpResponseRedirect 构造函数只接受一个参数-重定向到的路径.这可以是标准网址(例如," 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.

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

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