ImportError cannot import name render to response from django shortcuts

0 votes

After upgrading to Django 3.0, I get the following error:

ImportError: cannot import name 'render_to_response' from 'django.shortcuts'

My view:

from django.shortcuts import render_to_response

from django.template import RequestContext

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

Here is the full traceback:

Traceback (most recent call last):
  File "./", line 21, in <module>
  File "./", line 17, in main
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/", line 381, in execute_from_command_line
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/", line 375, in execute
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/commands/", line 60, in execute
    super().execute(*args, **options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/commands/", line 95, in handle**options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/commands/", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/", line 580, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/", line 565, in start_django
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/", line 272, in run
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/", line 48, in __get__
    res = instance.__dict__[] = self.func(instance)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/urls/", line 572, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/alasdair/.pyenv/versions/3.7.2/lib/python3.7/importlib/", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module

How to solve this error?

Jul 2, 2020 in Python by kartik
• 37,530 points

1 answer to this question.

0 votes

Hello @kartik,

The render_to_response shortcut was deprecated in Django 2.0, and is removed in Django 3.0. You can use the render shortcut instead, which was added way back in Django 1.3. The render shortcut works similarly to render_to_response, but takes request as its first argument. Change your view as follows:

from django.shortcuts import render

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

In your view, you have context_instance=RequestContext(request) as the third argument. This was deprecated in Django 1.8, and does not work in Django 1.10+.

If you are using render_to_response without context_instance, then you can pass None as the request to the render shortcut. For example, if you have,

return render_to_response('index.html', context)

then the equivalent with render is:

return render(None, 'index.html', context)

Hope it helps!!

answered Jul 2, 2020 by Niroj
• 82,820 points

Related Questions In Python

0 votes
1 answer

ImportError: cannot import name 'PILLOW_VERSION' from 'PIL' (unknown location)

Hi@akhtar, I think there is a problem in ...READ MORE

answered Apr 22, 2020 in Python by MD
• 95,300 points
0 votes
1 answer

ImportError: cannot import name 'API_KEY' from 'forwarder'

The AWS package in PyPI is not AWS's official ...READ MORE

answered Nov 6, 2020 in Python by Gitika
• 65,950 points
0 votes
1 answer

How to resolve ImportError: cannot import name 'add' in python ?

There's no module called "add" in random. ...READ MORE

answered Feb 13, 2019 in Python by Mark
0 votes
1 answer

ImportError: cannot import name 'main' in pip

Hi@akhtar, To avoid this error, you should upgrade ...READ MORE

answered Apr 15, 2020 in Python by MD
• 95,300 points
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,820 points
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,820 points
0 votes
1 answer

ImportError: cannot import name find_spec” when I start a new Django project?

Hello @kartik, find_spec isn't available in Python 3.2.3; it ...READ MORE

answered Aug 5, 2020 in Python by Niroj
• 82,820 points