render httprequest

render
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")

这就是使用 Django 模板系统的基本规则: 写模板,创建 Template 对象,创建 Context , 调用 render() 方法

 
 
 return render(request, "message.html", {"message": u"待办事项添加失败"})
 
 返回到render中指定的message.html页面 并且以字典的方式传值到该模板中比如:{"message": u"待办事项添加失败"}
 
 
redirect
return redirect(resolve_url( 'index' ))可以通过redirect直接进行跳转,不过写的是绝对路径,所以这种相对要好一点

 

return redirect('/index/') 效果同样

HttpRequest

返回参数给前端 {"message": u"待办事项添加失败"}

 

Django之HttpRequest和HttpResponse

 
Django之HttpRequest和HttpResponse - songyalong1117 - 宋亚龙


render

Combines a given template with a given context dictionary and returns an  HttpResponse  object with that rendered text.
我认为是返回渲染过的html页面和一些变量,还有HttpResponse object
 

render_to_response

Renders a given template with a given context dictionary and returns an HttpResponse object with that rendered text.
返回html页面和HttpResponse object
 
 
  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).

  2. HttpResponseRedirect("http://example.com/"):
    will create a new HttpResponse object with HTTP code 302 (Found/Moved temporarily). This should be used only to redirect to another page

    Renders a given template with a given context dictionary and returns an HttpResponse object with that rendered text.

 
  通过 
  
httpresponse render_to_response与render返回数据给前端页面,一般返回html(模板)给前端,也可以返回其他数据




 





转载于:https://www.cnblogs.com/wuqingzangyue/p/5749506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值