博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
render httprequest
阅读量:7082 次
发布时间:2019-06-28

本文共 1650 字,大约阅读时间需要 5 分钟。

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 
 object with that rendered text.
我认为是返回渲染过的html页面和一些变量,还有HttpResponse object
 

render_to_response

Renders a given template with a given context dictionary and returns an  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  object with that rendered text.

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

 

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

你可能感兴趣的文章
SSL/TLS协议安全系列:CBC 模式的弱安全性介绍(一)
查看>>
几种通用防注入程序绕过方法
查看>>
Clickjacking简单介绍
查看>>
Android Tangram模型:手把手带你学习淘宝、天猫都在用的UI框架模型
查看>>
《JavaScript设计模式与开发实践》基础篇(1)—— this、call 和 apply
查看>>
Android TransactionTooLargeException 解析,思考与监控方案
查看>>
Android音频处理知识(一)MediaRecorder录制音频
查看>>
SpringBoot+Vue.js前后端分离实现大文件分块上传
查看>>
Node.js环境性能监控
查看>>
CSS在没有设置高度的情况下如何让同级元素高度相等?
查看>>
Elastic Stack学习--elasticsearch部署常见问题
查看>>
Oracle 手工清理临时段
查看>>
通过git远程管理自己本地的工程
查看>>
scala中的下划线_
查看>>
QTreeWidget 获取被双击的子项的层次路径
查看>>
如何调整工作激情
查看>>
数据仓库专题(10)-文本事实和杂项维度
查看>>
VC6下实现remove_reference的方法。
查看>>
数据备份和还原
查看>>
Angular企业级开发(3)-Angular MVC实现
查看>>