Django error: render_to_response() got an unexpected keyword argument 'context_instance'

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:


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

    File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/" 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/" in _get_response
      185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "/Users/alasdair/dev/rtr/rtr/" 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.


Back to homepage or read more recommendations: