django HttpResponse
2018-02-26 13:45
302 查看
快捷函数
一般而言我们可以将渲染后的字符串作为 HttpResponse的第一个参数,作为构建响应报文的主体。
由于这个动作实在太常有了:加载--渲染--返回;所以django提供了两个快捷函数来处理这些事务。
这两个位于 django.shortcuts 模块中,在使用前记得先导入。
render(request, template_name[, context][, context_instance][, content_type][, status][, current_app][, dirs][, using])
这个函数实现查找,加载,渲染,构建 HttpResponse 对象一整套流程,所以我们可以使用它来节省许多功夫。
request:用于生成 response 对象的 request 对象。
template_name:模板名称,或者是包含许多模板名称的列表。
context:渲染使用的python字典。
context_instance:1.8版本起废弃,这里不再讨论。
content_type:指定 MIME 类型,默认为 DEFAULT_CONTENT_TYPE 的值。
status:响应的状态码,默认为200.
current_app:指示哪个应用包含当前的视图。1.8版本后废弃,现在要设置 request.current_app 进行代替。
using:用于加载模板使用的模板引擎的名称。
例子:
from django.shortcuts import render
def my_view(request):
# View code here...
return render(request, 'myapp/index.html', {"foo": "bar"},
content_type="application/xhtml+xml")
这个示例等同于:
from django.http import HttpResponse
from django.template import RequestContext, loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/index.html')
c = RequestContext(request, {'foo': 'bar'})
return HttpResponse(t.render(c),
content_type="application/xhtml+xml")
2. render_to_response(template_name[, context][, context_instance][, content_type][, status][, dirs][, using])
参数和上面的一样,就是少了 request 参数。
例子:
from django.shortcuts import render_to_response
def my_view(request):
# View code here...
return render_to_response('myapp/index.html', {"foo": "bar"},
content_type="application/xhtml+xml")
这个示例等同于:
from django.http import HttpResponse
from django.template import Context, loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/index.html')
c = Context({'foo': 'bar'})
return HttpResponse(t.render(c),
content_type="application/xhtml+xml")
一般而言我们可以将渲染后的字符串作为 HttpResponse的第一个参数,作为构建响应报文的主体。
由于这个动作实在太常有了:加载--渲染--返回;所以django提供了两个快捷函数来处理这些事务。
这两个位于 django.shortcuts 模块中,在使用前记得先导入。
render(request, template_name[, context][, context_instance][, content_type][, status][, current_app][, dirs][, using])
这个函数实现查找,加载,渲染,构建 HttpResponse 对象一整套流程,所以我们可以使用它来节省许多功夫。
request:用于生成 response 对象的 request 对象。
template_name:模板名称,或者是包含许多模板名称的列表。
context:渲染使用的python字典。
context_instance:1.8版本起废弃,这里不再讨论。
content_type:指定 MIME 类型,默认为 DEFAULT_CONTENT_TYPE 的值。
status:响应的状态码,默认为200.
current_app:指示哪个应用包含当前的视图。1.8版本后废弃,现在要设置 request.current_app 进行代替。
using:用于加载模板使用的模板引擎的名称。
例子:
from django.shortcuts import render
def my_view(request):
# View code here...
return render(request, 'myapp/index.html', {"foo": "bar"},
content_type="application/xhtml+xml")
这个示例等同于:
from django.http import HttpResponse
from django.template import RequestContext, loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/index.html')
c = RequestContext(request, {'foo': 'bar'})
return HttpResponse(t.render(c),
content_type="application/xhtml+xml")
2. render_to_response(template_name[, context][, context_instance][, content_type][, status][, dirs][, using])
参数和上面的一样,就是少了 request 参数。
例子:
from django.shortcuts import render_to_response
def my_view(request):
# View code here...
return render_to_response('myapp/index.html', {"foo": "bar"},
content_type="application/xhtml+xml")
这个示例等同于:
from django.http import HttpResponse
from django.template import Context, loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/index.html')
c = Context({'foo': 'bar'})
return HttpResponse(t.render(c),
content_type="application/xhtml+xml")
相关文章推荐
- Django——HttpRequest和HttpResponse
- Django使用HttpResponse返回图片并显示
- django之httpresponse
- Django网站建设-ModelFrom,url分发,HttpResponse配合Ajax,模板复用,重定向
- Django JsonResponse与HttpResponse重要区别
- Django1.7b版本中HttpResponse中 移除了参数mimetype=’application/json’ 这样的写法
- Django http: response render redirct
- django函数HttpResponse
- django中文件下载(HttpResponse)
- Django中视图总结[urls匹配,HttpRequest对象,HttpResponse,render,redirect对象,对象序列化接受及案例]
- Django使用HttpResponse返回图片并显示的方法
- Django——20141014深入理解Django HttpRequest HttpResponse的类和实例
- python web开发——Django HttpRequest和HttpResponse
- pyhon中Django框架的StreamingHttpResponse和HttpResponse
- Django中HttpRequest和HttpResponse
- from django.http import HttpResponseRedirect
- django views视图函数返回值 return redirect httpresponse总结
- django学习——通过HttpResponseRedirect 和 reverse实现重定向
- django httpresponse status code
- Django-- HttpResponse