所以我正试图为我的项目创建一个Django后端。这是我第一次做这样的事情,所以当我遇到CORS错误时(CORS策略:请求的资源上不存在"Access Control Allow Origin"标头。如果不透明的响应满足您的需求,请将请求的模式设置为"No CORS",以在禁用CORS的情况下获取资源。(我在谷歌上搜索了该怎么做。在完成了文档中的步骤之后,我在尝试运行"python manage.py runserver"时出现了以下错误。
C:UsersBenceDocumentsProgramozás-tanulásweb50final projectbenefactum>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:Python310libthreading.py", line 1009, in _bootstrap_inner
self.run()
File "C:Python310libthreading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "C:UsersBenceAppDataRoamingPythonPython310site-packagesdjangoutilsautoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:UsersBenceAppDataRoamingPythonPython310site-packagesdjangocoremanagementcommandsrunserver.py", line 115, in inner_run
autoreload.raise_last_exception()
File "C:UsersBenceAppDataRoamingPythonPython310site-packagesdjangoutilsautoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:UsersBenceAppDataRoamingPythonPython310site-packagesdjangocoremanagement__init__.py", line 381, in execute
autoreload.check_errors(django.setup)()
File "C:UsersBenceAppDataRoamingPythonPython310site-packagesdjangoutilsautoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:UsersBenceAppDataRoamingPythonPython310site-packagesdjango__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:UsersBenceAppDataRoamingPythonPython310site-packagesdjangoappsregistry.py", line 114, in populate
app_config.import_models()
File "C:UsersBenceAppDataRoamingPythonPython310site-packagesdjangoappsconfig.py", line 300, in import_models
self.models_module = import_module(models_module_name)
File "C:Python310libimportlib__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:Python310libsite-packagescorsheadersmodels.py", line 4, in <module>
from .signals import check_request_enabled # noqa
File "C:Python310libsite-packagescorsheaderssignals.py", line 5, in <module>
check_request_enabled = django.dispatch.Signal(
TypeError: Signal.__init__() got an unexpected keyword argument 'providing_args'
我已经按照文档完成了以下步骤:
- 从pip安装它(pip-install-djano-cors-headers(
- 我已经在INSTALLED_APPS中添加了"corsheaders"(在settings.py中(
- 添加";corssheaders.middleware"django.middleware.common.CommonMiddleware"到MIDDLEWARE(在settings.py中,根据文档建议将它们放得尽可能高(
- 已添加CORS_ALLOW_ALL_ORIGNS=True
这个问题甚至与我添加cors头有关吗?我只是假设,因为它在我之前就起作用了。如有任何帮助,我们将不胜感激!
在Django 4.0中删除了providing_args
kwarg。
如果你已经正确安装了django-cors-headers 3.10.1,这是不可能发生的,因为这在3.3中已经修复。此外,回溯中的行在库的3.10.1版本中不存在。
- 请确保您没有安装多个不同版本的库,例如一个在virtualenv内,另一个在它之外(很明显,您似乎有一个在virtualenv外(。
- 事实上,您似乎总共安装了两个Python 3.10,一个在
AppDataRoamingPythonPython310
,一个位于C:Python310
。你会想先把它清理干净
- 事实上,您似乎总共安装了两个Python 3.10,一个在
- 如果您不使用virtualenv,请开始使用它们来将不同项目的依赖关系彼此分离
答案:
转到您的Python安装文件夹->Lib->站点包->紧身胸衣->signal.py文件。(对我来说,它是C:\Python310\Lib\site-packages\corsheads\signal.py(
我通过将文件更改为以下内容来解决问题:
from django.dispatch import Signal
# Return Truthy values to enable a specific request.
# This allows users to build custom logic into the request handling
check_request_enabled = Signal()
更多细节和更智能的解释:https://github.com/django-notifications/django-notifications/issues/322