安装 cors-headers 后尝试 运行 服务器的 Django 错误
Django error trying to run server after installing cors-headers
所以我正在尝试为我的项目制作一个 Django back-end。这是我第一次做这样的事情,所以当我收到 CORS 错误时(CORS 策略:请求的资源上不存在 'Access-Control-Allow-Origin' header。如果不透明的响应满足您的需求,请设置请求的模式'no-cors' 以获取禁用 CORS 的资源。)我用 Google 搜索了该怎么做。
按照文档执行步骤后,尝试 运行 'python manage.py runserver'.
时出现以下错误
C:\Users\Bence\Documents\Programozás-tanulás\web50\final project\benefactum>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Python310\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\Python310\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\core\management\commands\runserver.py", line 115, in inner_run
autoreload.raise_last_exception()
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\core\management\__init__.py", line 381, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\apps\config.py", line 300, in import_models
self.models_module = import_module(models_module_name)
File "C:\Python310\lib\importlib\__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:\Python310\lib\site-packages\corsheaders\models.py", line 4, in <module>
from .signals import check_request_enabled # noqa
File "C:\Python310\lib\site-packages\corsheaders\signals.py", line 5, in <module>
check_request_enabled = django.dispatch.Signal(
TypeError: Signal.__init__() got an unexpected keyword argument 'providing_args'
我已经按照文档完成了以下步骤:
- 从 pip 安装它(pip install django-cors-headers)
- 我已将 'corsheaders' 添加到 INSTALLED_APPS(在 settings.py 中)
- 将“corsheaders.middleware.CorsMiddleware”、“django.middleware.common.CommonMiddleware”添加到 MIDDLEWARE(在 settings.py 中,根据文档建议将它们放在尽可能高的位置)
- 已添加 CORS_ALLOW_ALL_ORIGINS = 正确
问题是否与我添加 cors-headers 有关?我只是假设,因为它在我之前就开始工作了。任何帮助将不胜感激!
答案:
转到 Python 安装文件夹 -> Lib -> 站点包 -> corsheaders -> signal.py 文件。 (对我来说是 C:\Python310\Lib\site-packages\corsheaders\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
Django 4.0 中删除了 providing_args
kwarg。
如果您正确安装了 django-cors-headers 3.10.1,就不可能发生这种情况,因为 this was fixed in 3.3. In addition, the line in your traceback does not exist in version 3.10.1 of the library.
- 确保您没有安装多个不同版本的库,例如一个在 virtualenv 中,一个在它之外(你显然在 virtualenv 之外有一个)。
- 事实上,您似乎总共有 两个 Python 3.10 安装,一个在
AppData\Roaming\Python\Python310
中,一个在 C:\Python310
中。你会想先把它清理干净。
- 如果您不使用 virtualenvs,请开始使用它们来分离不同项目的依赖关系。
所以我正在尝试为我的项目制作一个 Django back-end。这是我第一次做这样的事情,所以当我收到 CORS 错误时(CORS 策略:请求的资源上不存在 'Access-Control-Allow-Origin' header。如果不透明的响应满足您的需求,请设置请求的模式'no-cors' 以获取禁用 CORS 的资源。)我用 Google 搜索了该怎么做。 按照文档执行步骤后,尝试 运行 'python manage.py runserver'.
时出现以下错误 C:\Users\Bence\Documents\Programozás-tanulás\web50\final project\benefactum>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Python310\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\Python310\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\core\management\commands\runserver.py", line 115, in inner_run
autoreload.raise_last_exception()
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\core\management\__init__.py", line 381, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Bence\AppData\Roaming\Python\Python310\site-packages\django\apps\config.py", line 300, in import_models
self.models_module = import_module(models_module_name)
File "C:\Python310\lib\importlib\__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:\Python310\lib\site-packages\corsheaders\models.py", line 4, in <module>
from .signals import check_request_enabled # noqa
File "C:\Python310\lib\site-packages\corsheaders\signals.py", line 5, in <module>
check_request_enabled = django.dispatch.Signal(
TypeError: Signal.__init__() got an unexpected keyword argument 'providing_args'
我已经按照文档完成了以下步骤:
- 从 pip 安装它(pip install django-cors-headers)
- 我已将 'corsheaders' 添加到 INSTALLED_APPS(在 settings.py 中)
- 将“corsheaders.middleware.CorsMiddleware”、“django.middleware.common.CommonMiddleware”添加到 MIDDLEWARE(在 settings.py 中,根据文档建议将它们放在尽可能高的位置)
- 已添加 CORS_ALLOW_ALL_ORIGINS = 正确
问题是否与我添加 cors-headers 有关?我只是假设,因为它在我之前就开始工作了。任何帮助将不胜感激!
答案:
转到 Python 安装文件夹 -> Lib -> 站点包 -> corsheaders -> signal.py 文件。 (对我来说是 C:\Python310\Lib\site-packages\corsheaders\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
Django 4.0 中删除了 providing_args
kwarg。
如果您正确安装了 django-cors-headers 3.10.1,就不可能发生这种情况,因为 this was fixed in 3.3. In addition, the line in your traceback does not exist in version 3.10.1 of the library.
- 确保您没有安装多个不同版本的库,例如一个在 virtualenv 中,一个在它之外(你显然在 virtualenv 之外有一个)。
- 事实上,您似乎总共有 两个 Python 3.10 安装,一个在
AppData\Roaming\Python\Python310
中,一个在C:\Python310
中。你会想先把它清理干净。
- 事实上,您似乎总共有 两个 Python 3.10 安装,一个在
- 如果您不使用 virtualenvs,请开始使用它们来分离不同项目的依赖关系。