【问题标题】:Django ImportError: cannot import name 'render_to_response' from 'django.shortcuts'Django ImportError:无法从“django.shortcuts”导入名称“render_to_response”
【发布时间】:2019-09-18 13:18:46
【问题描述】:

升级到 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)

【问题讨论】:

    标签: python django python-3.x django-views django-3.0


    【解决方案1】:

    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

    【讨论】:

    • 我对答案没有异议,产生了错误,但我注意到从 Django 3.2 开始,render_to_response still 实际上是 in 快捷方式, fwiw。
    • @MalikA.Rumi 不,该快捷方式不在 Django 3.2 中,您可以查看 shortcuts.py 代码 here。您可能会将其与基于类的视图中的 render_to_response method 混淆。
    • 你有很多我做的点。所以你可能是对的。昨晚 vs code 向我展示了一个文件“shortcuts.pyi”。我注意到最后的“i”,但没有多想。今天早上,我无法让 vs 代码向我展示任何东西,所以我自己挖掘,这次找到了“shortcuts.py”,正如你所描述的那样。感谢您的额外课程! ;-)
    • shortcuts.pyi 是一个存根文件,vs 代码扩展使用它来帮助自动完成和检测错误。该文件不是您的 Django 安装的一部分。
    猜你喜欢
    • 1970-01-01
    • 2022-06-24
    • 2018-05-29
    • 1970-01-01
    • 2015-04-30
    • 2019-08-27
    • 2016-09-07
    • 2018-11-15
    • 1970-01-01
    相关资源
    最近更新 更多