Django错误:render_to_response()获得了意外的关键字参数“ context_instance”


70

升级到Django 1.10后,出现错误render_to_response() got an unexpected keyword argument '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))

这是完整的回溯:

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'

Answers:


134

中的context_instance参数在Django 1.8render_to_response弃用,在Django 1.10中已删除。

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

更新您的导入并按如下所示进行查看。注意,renderrequest对象作为其第一个参数。

from django.shortcuts import render

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

render快捷方式是Django 1.3中引入的,因此此更改与Django的较早版本兼容。


2

在Django 1.8中兼容:

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}, context_instance=RequestContext(request))

在Django 1.11中

from django.shortcuts import render
from django.template import RequestContext

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

您无需render_to_response在Django 1.8中使用。就像我在回答中说的那样,该render版本适用于Django 1.3+。
阿拉斯代尔

在django 1.8中还兼容:render_to_response,在virtualenv中为我检查过。不过,在django的最新版本中是带有render的。
Dantez Layton '18

是的,render_to_response可以在1.8中使用,但是没有充分的理由使用它-render更简单。
Alasdair

您的render_to_response示例中有一个错误。只是使用context而不是{'context': context}
阿拉斯代尔

0

Django 2.0.7 Python 3.5

在project / urls.py中

handler404 = 'project.views.not_found'
handler500 = 'project.views.server_error'

在project / views.py中

def not_found(request, exception=None):
response = render(request, '404.html', {})
response.status_code = 404
return response

def server_error(request, exception=None):
response = render(request, '500.html', {})
response.status_code = 500
return response

exception = None不传递,因为否则会引发TypeError:server_error()缺少1个必需的位置参数:'exception'


1
我不确定您为什么将其添加为该问题的答案,它似乎与无关render_to_response
阿拉斯代尔
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.