Django错误:render_to_response()得到了一个意想不到的关键字参数'context_instance' [英] Django error: render_to_response() got an unexpected keyword argument 'context_instance'

查看:600
本文介绍了Django错误:render_to_response()得到了一个意想不到的关键字参数'context_instance'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到Django 1.10后,我收到错误 render_to_response()得到一个意想不到的关键字参数context_instance



我的观点如下:

  from django.shortcuts import render_to_response 
from django.template import RequestContext

def my_view(request):
context = {'foo':'bar'}
return render_to_response('my_template.html',context,context_instance = RequestContext(request)

这是完整的追溯:

 追溯:

文件/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/exception.py in inner
39. response = get_response(request)

文件/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers /base.py_get_response
187. response = self.process_exception_by_middleware(e,request)

文件/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/base.py在_get_response
185. response = wrapped_callback(request, * callback_args,** callback_kwargs)

my_view
中的/Users/alasdair/dev/rtr/rtr/urls.py文件26.返回render_to_response('my_template.html',上下文,context_instance = RequestContext(request))

异常类型:TypeError at /
异常值:render_to_response()得到一个意想不到的关键字参数'context_instance'


解决方案

<$ c $中的 context_instance c> render_to_response 是在Django 1.8中弃用,并在Django 1.10中删除。



解决方案是切换到 render 快捷方式,自动使用 RequestContext



更新您的导入和查看,如下所示。请注意, render 请求对象作为第一个参数。



pre $ 从django.shortcuts import render

def my_view(request):
context = {'foo':'bar'}
return render(request,'my_template.html',context)

渲染快捷方式在Django 1.3中引入,所以这个更改与旧版本的Django兼容。


After upgrading to Django 1.10, I get the error render_to_response() got an unexpected keyword argument 'context_instance'.

My view is as follows:

from django.shortcuts import render_to_response
from django.template import RequestContext

def my_view(request):
    context = {'foo': 'bar'}
    return render_to_response('my_template.html', context, context_instance=RequestContext(request))

Here is the full traceback:

Traceback:

File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/alasdair/dev/rtr/rtr/urls.py" in my_view
  26.     return render_to_response('my_template.html', context,  context_instance=RequestContext(request))

 Exception Type: TypeError at /
Exception Value: render_to_response() got an unexpected keyword argument 'context_instance'

解决方案

The context_instance parameter in render_to_response was deprecated in Django 1.8, and removed in Django 1.10.

The solution is to switch to the render shortcut, which automatically uses a RequestContext.

Update your imports and view as follows. Note that render takes the request object as its first argument.

from django.shortcuts import render

def my_view(request):
    context = {'foo': 'bar'}
    return render(request, 'my_template.html', context)

The render shortcut was introduced in Django 1.3, so this change is compatible with older versions of Django.

这篇关于Django错误:render_to_response()得到了一个意想不到的关键字参数'context_instance'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆