升级到Django 3.0后,出现以下错误:

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

我的观点是:

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))

以下是完整的回溯:

Traceback (most recent call last):
  File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/base.py", 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/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 95, in handle
    self.run(**options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/autoreload.py", 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/autoreload.py", line 565, in start_django
    reloader.run(django_main_thread)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/autoreload.py", line 272, in run
    get_resolver().urlconf_module
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/urls/resolvers.py", line 572, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/alasdair/.pyenv/versions/3.7.2/lib/python3.7/importlib/__init__.py", 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
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/alasdair/dev/myproject/myproject/urls.py", line 19, in <module>
    from myapp import views
  File "/Users/alasdair/dev/myproject/myapp/views.py", line 8, in <module>
    from django.shortcuts import render_to_response
ImportError: cannot import name 'render_to_response' from 'django.shortcuts' (/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/shortcuts.py)

推荐答案

render_to_response捷径是deprecated in Django 2.0,是removed in Django 3.0.您可以使用render快捷方式,这是在Django 1.3中添加的.render快捷方式的工作原理与render_to_response类似,但第一个参数是request.按如下方式更改您的视图:

from django.shortcuts import render

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

在你看来,第三个论点是context_instance=RequestContext(request).这在Django 1.8中被弃用,在Django 1.10+中不起作用.

如果使用render_to_response而不使用context_instance,则可以将None作为请求传递给render快捷方式.例如,如果你有,

return render_to_response('index.html', context)

那么render的类似功能是:

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

请注意,如果将None作为第一个参数传递,那么模板将不带任何context processors.这可能会使渲染速度稍快一些,但可能会导致CSRF错误,这意味着您将无法从上下文处理器(例如{{ request }}{{ user }})访问变量,除非您显式地将它们添加到上下文中.除非你明白这些后果,否则我不建议你这样使用None.

Python-3.x相关问答推荐

TypeError:&Quot;Value&Quot;参数必须是标量、Dict或Series,但您传递了&Quot;Index&Quot;

Python将类实例变量转换为嵌套 struct

如何获得大Pandas 的常见时间间隔

Numpy argmin()以查找最近的元组

如何使用regex将电话号码和姓名从文本字符串中分离出来

丢弃重复的索引,并在多索引数据帧中保留一个

Django 模型类方法使用错误的 `self`

Django - ValueError:无法将字符串转换为浮点数:''

如果网站加载时间过长,如何强制 Selenium 刷新

在气流中运行 DAG 时出现处理信号:ttou消息

内部如何使用 Python 语法?

如何从左到右解包元组?

为 python3 安装 opencv

如何从同一文件夹中的模块导入功能?

Python3 mysqlclient-1.3.6(又名 PyMySQL)的用法?

if 语句中冒号的语法错误

迭代dict值

python 内置的 __exit__ 参数类型是什么?

如何在 QGraphicsView 中启用平移和zoom

python asyncio add_done_callback 与 async def