【问题标题】:ImportError: cannot import name 'render_to_response' from 'django.shortcuts' - Django 3.2 Python 3.8ImportError:无法从“django.shortcuts”导入名称“render_to_response” - Django 3.2 Python 3.8
【发布时间】:2022-01-20 14:45:13
【问题描述】:

我收到了这个错误

    Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
    autoreload.check_errors(django.setup)()
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/apps/registry.py", line 122, in populate
    app_config.ready()
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/contrib/admin/apps.py", line 27, in ready
    self.module.autodiscover()
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/kritik/empereon_django3.2/lib/python3.8/site-packages/djcelery/admin.py", line 11, in <module>
    from django.shortcuts import render_to_response
ImportError: cannot import name 'render_to_response' from 'django.shortcuts' (/home/kritik/empereon_django3.2/lib/python3.8/site-packages/django/shortcuts.py)

当我将 Django 从 2.2 升级到 3.2 时,我得到了这个我的 python 版本是 3.8。我没有在代码中的任何地方使用 render_to_response 是否与 python 版本有关?我无法理解 有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: python-3.x django django-rest-framework


    【解决方案1】:

    render_to_response 快捷方式在 Django 2.0 中已被弃用,并在 Django 3.0 中被删除。您可以改用渲染快捷方式,它是在 Django 1.3 中添加的。渲染快捷方式的工作方式与 render_to_response 类似,但将 request 作为其第一个参数。改变你的看法如下:

    from django.shortcuts import render
    

    而不是 render_to_response

    【讨论】:

    • 我没有在代码中的任何地方使用 render_to_response。
    • 检查 admin.py 第 11 行
    • 发现错误。 django-celery 版本存在问题,我更新了它并解决了错误。感谢您的帮助。
    猜你喜欢
    • 2019-09-18
    • 2016-01-07
    • 1970-01-01
    • 2022-06-24
    • 2021-12-19
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多