Подтвердить что ты не робот

Ошибка Django: render_to_response() получил неожиданный аргумент ключевого слова 'context_instance'

После обновления до 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'
4b9b3361

Ответ 1

Параметр context_instance в render_to_response был устарел в Django 1.8 и удален в Django 1.10.

Решение состоит в том, чтобы переключиться на render ярлык, который автоматически использует RequestContext.

Обновите свой импорт и просмотрите его следующим образом. Обратите внимание, что render принимает в качестве первого аргумента объект request.

from django.shortcuts import render

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

Ярлык render был введен в Django 1.3, поэтому это изменение совместимо со старыми версиями Django.