render() must be called with a dict, not a Context. return HttpResponse(t.render(c))解决
来源:互联网 发布:sql删除重复记录 编辑:程序博客网 时间:2024/04/30 00:10
报错说render()只能传入字典
错误的代码:
def index(request): t=loader.get_template("index.html") title={"title":"hello Django"} user={"name":"LCY","age":"20"} context=Context({"title":title,"user":user}) return HttpResponse(t.render(context))
报错定位在return HttpResponse(t.render(context))
后来查看了下Context的原码是这样写的、
class Context(BaseContext): "A stack container for variable context" def __init__(self, dict_=None, autoescape=True, use_l10n=None, use_tz=None): self.autoescape = autoescape self.use_l10n = use_l10n self.use_tz = use_tz self.template_name = "unknown" self.render_context = RenderContext() # Set to the original template -- as opposed to extended or included # templates -- during rendering, see bind_template. self.template = None super(Context, self).__init__(dict_) @contextmanager def bind_template(self, template): if self.template is not None: raise RuntimeError("Context is already bound to a template") self.template = template try: yield finally: self.template = None def __copy__(self): duplicate = super(Context, self).__copy__() duplicate.render_context = copy(self.render_context) return duplicate def update(self, other_dict): "Pushes other_dict to the stack of dictionaries in the Context" if not hasattr(other_dict, '__getitem__'): raise TypeError('other_dict must be a mapping (dictionary-like) object.') if isinstance(other_dict, BaseContext): other_dict = other_dict.dicts[1:].pop() return ContextDict(self, other_dict)
从原码中可以看到,当直接使用Context()的时候传入的值无法返回dict,调用update()方法就可以所以根据以上思路修改后为:
def index(request): t=loader.get_template("index.html") title={"title":"hello Django"} user={"name":"LCY","age":"20"} context=Context().update({"title":title,"user":user}) return HttpResponse(t.render(context))
这样就解决了
阅读全文
0 0
- render() must be called with a dict, not a Context. return HttpResponse(t.render(c))解决
- TypeError: context must be a dict rather than Context.
- 解决vux 引入组件时Error in render function: "TypeError: _vm.$t is not a function"问题
- React.render is not a function
- laydate.render is not a function
- upload.render is not a function
- Django 中遇到的问题(1)TypeError: context must be a dict rather than Context.
- TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)
- Python异常:Django1.11--context must be a dict rather than RequestContext.
- A context path must either be an empty string or start with a '/' and do not end with a '/' 问题解决方法
- why vfree() must not be called in interrupt context?
- The function equals must be used with a prefix when a default namespace is not specified解决
- 关于The function equals must be used with a prefix when a default namespace is not specified解决
- 关于The function equals must be used with a prefix when a default namespace is not specified解决
- 关于The function size must be used with a prefix when a default namespace is not specified解决
- Generic detail view DetailView must be called with either an object pk or a slug.
- Function importPackage must be called with a package; had "[JavaClass java.util.ArrayList]"
- Function importPackage must be called with a package; had "[JavaClass java.util.ArrayList]"
- Activiti——数据表结构
- MarkdownPad 2在Window10系统下解决无法使用的问题
- synchronized三种使用方式
- 通用块层与bio
- jQuery id选择器无效
- render() must be called with a dict, not a Context. return HttpResponse(t.render(c))解决
- (分享)win10下双显示屏独立设置不同缩放率的方法
- 814D
- jquery通过插入方法向html中插入对象,对象的hover失效解决办法
- 常用数学公式
- 你不得不了解的JVM
- 机器学习-(2):无监督学习
- 即时通讯 与 网络相关
- java相关优秀博文收藏