在Django中template遇到 "context must be a dict rather
原代码:
# 使用模板文件
# 1.加载模板文件,获取一个模板文件
temp = loader.get_template('booktest/index.html') # 2.定义模板上下文:给模板文件传递数据
context = RequestContext(request, {}) # 3.模板渲染:产生标准的html内容
res_html = temp.render(context) # 4.返回给浏览器
return HttpResponse(res_html)
错误提示:
Exception Type: TypeError at /index
Exception Value: context must be a dict rather than RequestContext.
正确代码: # 1.加载模板文件
temp = loader.get_template('booktest/index.html') # 2.定义模板上下文:给模板文件传递数据
context = RequestContext(request, {})
context.push(locals()) # 3.模板渲染:产生标准的html内容
res_html = temp.render(context=locals(), request=request) # 4.返回给浏览器
return HttpResponse(res_html)
原代码:
# 使用模板文件
# 1.加载模板文件
temp = loader.get_template('booktest/index.html')
# 2.定义模板上下文:给模板文件传递数据
context = RequestContext(request, {})
# 3.模板渲染:产生标准的html内容
res_html = temp.render(context)
# 4.返回给浏览器
return HttpResponse(res_html)
错误提示:
Exception Type: TypeError at /index
Exception Value: context must be a dict rather than RequestContext.
正确代码:
# 1.加载模板文件
temp = loader.get_template('booktest/index.html') # 2.定义模板上下文:给模板文件传递数据
context = RequestContext(request, {})
context.push(locals()) # 3.模板渲染:产生标准的html内容
res_html = temp.render(context=locals(), request=request) # 4.返回给浏览器
return HttpResponse(res_html)
在Django中template遇到 "context must be a dict rather的更多相关文章
- django中Template语言
Template本身也有自己的语言和语法,用来处理简单的数据显示 常用语法 判断指令 {% if 条件 %}...{%endif%} {% if 条件 %}...{%elif 条件 %}...{%en ...
- django 中的render和render_to_response()和locals()
1. django中的render context在Django里表现为 Context 类,在 django.template 模块里. 它的构造函数带有一个可选的参数: 一个字典映射变量和它们的值 ...
- Django中全局Context处理器
1.模板标签和模板变量 模板标签在{% %}中定义: {% if is_logged_in %} Thanks for logging in! {% else %} Please log in. {% ...
- django中的template部分
模版部分 取值 {{key}} 语句块 {%for item in data%} {%extends 'base.html'%} 深度变量的查找(万能的句点号) 在到目前为止的例子中,我们通过 con ...
- 自定义django的Template context processors
简要步骤: 1.编辑一个函数: def media_url(request): from django.conf import settings return {'media_url': settin ...
- python框架Django中MTV框架之Template(模板/界面)
MTV框架之Template(模板/界面) 关注公众号"轻松学编程"了解更多. 1.模板目录位置 应用下 不需要注册 无法跨应用地进行复用 工程下 需要注册 settings.py ...
- Django 中 如何使用 settings.py 中的常量
在用django 框架开发 python web 程序的时候 , 在模板页面经常会用到 settings.py 中设置的常量,比如MEDIA_URL, 我尝试过在模板页面用类似如下的方式 程序代码 { ...
- django中自定义标签和过滤器
想要实现自定义标签和过滤器需要进行准备工作: 准备(必需)工作: 1 在某个app下创建一个名为templatetags(必需,且包名不可变)的包.假设我们在名为polls的app下创建了一个tem ...
- <django中render_to_response的可选参数和使用方法>
在django官方文档中有比较详细的介绍,在此我按照自己的理解适当的阐述一下: return render_to_response(①'my_template.html', ②my_data_dict ...
随机推荐
- 布局 Bootstrap Table的 文本内容 垂直居中
原文:https://blog.csdn.net/peng_hong_fu/article/details/70662979 样式(注意样式优先级): #div-component-info .tab ...
- sql语句之where与having的区别
where条件查询: 使用 where 可以将表中符合条件的数据筛选出来后,得到查询结果. 语法: select 字段名,……,字段名 from 表名 where 条件表达式; 例: 注意:在条件表达 ...
- threejs行星运动小demo总结
1.动画构思 就是中间有个红太阳,外面有几个行星球体环绕着太阳在各自轨道上做圆周运动.下面是效果图 2.基本要素 使用threejs的基本构件包括:渲染器(renderer),相机(camera),场 ...
- VMware 桥接网络设置
1. 桥接的基本原理 桥接是将虚拟机和宿主机在局域网中的地位看成是一样的,逻辑如下: 但实际上是通过如下图实现的:(该图来自http://blog.csdn.net/qingfengtsing/art ...
- Windows下基于IIS服务的SSL服务器的配置
Windows下基于IIS服务的SSL服务器的配置 实验环境 Windows Server 2008 R1(CA) Windows Server 2008 R2(web服务器) Windows 7 x ...
- Perl寻路A*算法实现
A*算法:A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法.估价值与实际值越接近,估价函数取得就越好. 公式表示为: f(n)=g(n)+h(n),其中 f(n) 是从初始点经 ...
- shell获取今天、明天、昨天、n天、周、月、年日期
1.获取今天日期 $ date -d now +%Y-%m-%d 或者$ date +%F 1 2 2.获取明天日期 $ date -d next-day +%Y-%m-%d$ date - ...
- 主外键 设置 on update cascade 和on delete cascade 的区别
on update cascade 和on delete cascade 的区别 这是数据库外键定义的一个可选项,用来设置当主键表中的被参考列的数据发生变化时,外键表中响应字段的变换规则的.updat ...
- iptables规则保存
/etc/init.d/iptables save #查看 vim /etc/sysconfig/iptables #将iptables设置为开机启动 chkconfig iptables on #查 ...
- android studio 运行按钮为灰色的解决办法之一
sync project with gradle files按钮(如下图)同步一下就好了 3.2的 3.3同步按钮变成了一只大象+箭头