如果 render_to_response 方法没有包含在任何地方,为啥我会收到 ImportError?
Posted
技术标签:
【中文标题】如果 render_to_response 方法没有包含在任何地方,为啥我会收到 ImportError?【英文标题】:Why am I getting ImportError on render_to_response method if it's not included anywhere?如果 render_to_response 方法没有包含在任何地方,为什么我会收到 ImportError? 【发布时间】:2020-12-03 13:56:00 【问题描述】:我正在处理一个 Django 项目,当我想在本地运行它时,我得到以下信息:
ImportError: cannot import name 'render_to_response' from 'django.shortcuts'
但是,正如我检查的那样,我没有在项目的任何地方包含 render_to_response 方法。我相信这可能是任何库之间的不兼容问题,虽然我不能说。
我的python版本是3.7,我的Django版本是3.1,我的pip版本是20.2.2。
我的 pip freeze 返回:
allauth-django==0.42.6
asgiref==3.2.10
certifi==2020.6.20
chardet==3.0.4
defusedxml==0.6.0
Django==3.1
django-crispy-forms==1.9.2
django-filter==2.3.0
django-tables2==2.3.1
idna==2.10
mysqlclient==2.0.1
oauthlib==3.1.0
python3-openid==3.2.0
pytz==2020.1
requests==2.24.0
requests-oauthlib==1.3.0
sqlparse==0.3.1
tablib==2.0.0
urllib3==1.25.10
任何帮助将不胜感激。
完整的堆栈跟踪是这样的:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\IvanH\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\IvanH\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\IvanH\AppData\Local\Programs\Python\Python37\lib\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 "C:\Users\IvanH\PycharmProjects\sare_project\SareProject\urls.py", line 25, in <module>
path('formats/', include('formats.urls')),
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\IvanH\AppData\Local\Programs\Python\Python37\lib\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 "C:\Users\IvanH\PycharmProjects\sare_project\formats\urls.py", line 25, in <module>
path('accounts/', include('allauth.urls')),
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\IvanH\AppData\Local\Programs\Python\Python37\lib\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 "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\allauth\urls.py", line 5, in <module>
from allauth.django_utils.admin import *
File "C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\allauth\django_utils\admin.py", line 5, in <module>
from django.shortcuts import render_to_response
ImportError: cannot import name 'render_to_response' from 'django.shortcuts' (C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\django\shortcuts.py)
更新
当我尝试卸载它并安装 allauth-django 0.42.0 时,使用命令 pip install allauth-django==0.42.0
,我收到以下错误:
ERROR: Could not find a version that satisfies the requirement django-utils==0.4.7 (from allauth-django==0.42.0) (from versions: 0.0.1, 0.0.2)
ERROR: No matching distribution found for django-utils==0.4.7 (from allauth-django==0.42.0)
如果我只使用pip install allauth-django
,它会自动选择版本0.42.6。
【问题讨论】:
回溯显示导入在C:\Users\IvanH\PycharmProjects\sare_project\venv\lib\site-packages\allauth\django_utils\admin.py
。最新的allauth
版本是0.42.0
- 我很惊讶你的pip freeze
显示0.42.6
。我会尝试卸载allauth,然后重新安装。
当我这样做时,卸载它并使用 [pip install allauth-django==0.42.0] 重新安装,我得到这个:错误:找不到满足要求的版本 django-utils= =0.4.7(来自 allauth-django==0.42.0)(来自版本:0.0.1、0.0.2)错误:找不到 django-utils==0.4.7 的匹配分布(来自 allauth-django==0.42 .0)
但是当我只使用 [pip install allauth-django] 时它会自动选择 0.42.6 并且没有显示错误。
您应该查看这个答案,这可能会有所帮助:***.com/questions/55911435/…
我看到了,但在那种情况下,他正在使用该方法,并且是使用另一个方法的问题,因为我知道 render_to_response 已被弃用,但在我的情况下,我认为这是一个包问题,而且我不知道 wny 在其内部代码中是 allauth,显然,导入了该方法,该方法已在 Django 3.0+ 中删除
【参考方案1】:
这可以通过三个步骤简单地解决,在你的终端上按照给定的顺序运行这些命令:
-
pip 卸载 allauth-django
pip 卸载 django-allauth
pip install django-allauth
你可以走了。
【讨论】:
以上是关于如果 render_to_response 方法没有包含在任何地方,为啥我会收到 ImportError?的主要内容,如果未能解决你的问题,请参考以下文章
django render_to_response 如何发送特殊字符
render_to_response 或 redirect 改变了 Django 1.8 中的模板元素
Django - render()、render_to_response() 和 direct_to_template() 有啥区别?