1. django中的render

context在Django里表现为 Context 类,在 django.template 模块里。 它的构造函数带有一个可选的参数:

一个字典映射变量和它们的值。 调用 Template 对象 的 render() 方法并传递context来填充模板:

>>> from django.template import Context, Template
>>> t = Template('My name is {{ name }}.')
>>> c = Context({'name': 'Stephane'})
>>> t.render(c)
u'My name is Stephane.'

在views.py中:

return render(request, 'blog_add.html', {'blog': blog, 'form': form, 'id': id, 'tag': tag})

2. django中的render_to_response

 return render_to_response('blog_add.html', {'blog': blog, 'form': form, 'id': id, 'tag': tag})

很明显,如果使用render_to_response就省去了render里传递的request。

3.locals()用法:locals()可以直接将函数中所有的变量全部传给模板。当然这可能会传递一些多余的参数,有点浪费内存的嫌疑。

return render(request, 'blog_add.html', locals())
return render_to_response('blog_add.html', locals())

django 中的render和render_to_response()和locals()的更多相关文章

  1. 毕业设计——django中的render()与redirect()问题

    1. redirect()时需要传递数据,在网上找到的方法是通过session传递数据,但是个人认为用session传递数据并不合适,session一般用于权限验证数据的传递... 2. render ...

  2. Django中render_to_response和render的区别(转载)

    转载地址:https://www.douban.com/note/278152737/ 自django1.3开始:render()方法是render_to_response的一个崭新的快捷方式,前者会 ...

  3. Django源码学习 了解render与render_to_response

    render与render_to_response def render_to_response(template_name, context=None, content_type=None, sta ...

  4. Django中views数据查询使用locals()函数进行优化

    优化场景 利用视图函数(views)查询数据之后可以通过上下文context.字典.列表等方式将数据传递给HTML模板,由template引擎接收数据并完成解析.但是通过context传递数据可能就存 ...

  5. Django中 render() 函数的使用方法

    render() 函数 在讲 render() 函数之前,我们在 Django 项目 index 文件夹的 urls.py 和 views.py 中编写如下功能代码:(不难,望读者细心阅之) # in ...

  6. django 中的视图(Views)

    Views Django中views里面的代码就是一个一个函数逻辑, 处理客户端(浏览器)发送的HTTPRequest, 然后返回HTTPResponse, http请求中产生两个核心对象: http ...

  7. Django中的form组件

    Django中的form组件有两大作用 1.验证获取正确的结果或者错误信息 2.生成html代码 一.为什么需要form组件呢? 在写form表单,提交数据时,自己写验证的代码是一件非常困难的事情. ...

  8. django中如何实现分页功能

    1.在html页面中导入js文件和css文件 <link rel="stylesheet" href="../../../static/css/jquery.pag ...

  9. Django中怎么做图片上传--图片展示

    1.首先是html页面的form表单的三大属性,action是提交到哪,method是提交方式,enctype只要有图片上传就要加这个属性 Django框架自带csrf_token ,所以需要在前端页 ...

随机推荐

  1. 《征服C指针》读书笔记

    本文同时发布在我的个人博客上,欢迎访问~ www.seekingdream.cn 在读完K&R之后,对C的认识就是指针.数组.网上的人们对指针也有些“敬而远之”的感觉.最近从同学处淘得< ...

  2. c++ 类前置声明【转】

    [转自 here] 在编写C++程序的时候,偶尔需要用到前置声明(Forward declaration).下面的程序中,带注释的那行就是类B的前置说明.这是必须的,因为类A中用到了类B,而类B的声明 ...

  3. c++ 纯虚析构函数

    ; 这就是一个纯虚析构函数,这种定义是允许的. 一般纯虚函数都不允许有实体,但是因为析构一个类的过程中会把所有的父类全析构了,所以每个类必有一个析构函数. 所以.纯虚析构函数需要提供函数的实现,而一般 ...

  4. 【Spring Boot && Spring Cloud系列】Spring Boot的启动器Starter

    Spring Boot的内置Servlet Container: Name Servlet Version Java Version Tomcat8 3.1 Java 7+ Tomcat7 3.0 J ...

  5. 使用import取代require

    首先,Module 语法是 JavaScript 模块的标准写法,坚持使用这种写法.使用import取代require. // bad const moduleA = require('moduleA ...

  6. Android 反编译Apk提取XML文件

    Apktool https://ibotpeaches.github.io/Apktool/install/ 下载地址:Apktool https://bitbucket.org/iBotPeache ...

  7. docker自动开启端口转发功能

    yum -y install epel-release yum -y install docker-io service docker start docker pull haproxy # 此时自动 ...

  8. 在python pydev中使用todo标注任务

    在做自动化测试时,有部分代码因需求未定或界面需要更改,代码不做修改或更新,这里就需要用到TODO功能. 在PyCharm中TODO功能很详细,但在pydev中怎么用呢.看了文档后,截图如下: 1.设置 ...

  9. Ubuntu 16.04系统下开机提示“无法应用原保存的显示器配置”

    开机启动Ubuntu时,提示以下错误,部分截图如图: 解决方法: 按住Ctrl+Alt+T开启终端,输入rm .config/monitors.xml,回车,然后重启Ubuntu即可解决:如图

  10. Git - could not read Username for 'https://github.com',push报错解决办法

    执行git push命令异常,如下: git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sou ...