简体   繁体   English

Django 在安装 cors-headers 后尝试运行服务器时出错

[英]Django error trying to run server after installing cors-headers

So I'm trying to make a Django back-end for my project.所以我正在尝试为我的项目制作一个 Django 后端。 It's my first time doing something like this, so when I got a CORS error (CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.) I Googled what to do.这是我第一次做这样的事情,所以当我收到 CORS 错误时(CORS 策略:没有 'Access-Control-Allow-Origin' header 出现在请求的资源上。如果不透明的响应满足您的需求,请设置请求的模式到“no-cors”以获取禁用 CORS 的资源。)我用谷歌搜索了该怎么做。 After doing the steps following the documentation, I've got the following error when trying to run 'python manage.py runserver'.在按照文档执行步骤后,尝试运行“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'

I've done the following steps, following the documentation:我按照文档完成了以下步骤:

  1. Installing it from pip (pip install django-cors-headers)从 pip 安装它(pip install django-cors-headers)
  2. I've added 'corsheaders' to the INSTALLED_APPS (in settings.py)我已将“corsheaders”添加到 INSTALLED_APPS(在 settings.py 中)
  3. Adding "corsheaders.middleware.CorsMiddleware", "django.middleware.common.CommonMiddleware" to MIDDLEWARE (in settings.py, putting them as high as possible as per document recommendation)将“corsheaders.middleware.CorsMiddleware”、“django.middleware.common.CommonMiddleware”添加到 MIDDLEWARE(在 settings.py 中,根据文档建议将它们尽可能高地放置)
  4. Added CORS_ALLOW_ALL_ORIGINS = True添加 CORS_ALLOW_ALL_ORIGINS = True

Is the problem even related to me adding cors-headers?这个问题甚至与我添加 cors-headers 有关吗? I'm only assuming, because it was working before I did.我只是假设,因为它在我之前就开始工作了。 Any help would be greatly appreciated!任何帮助将不胜感激!

Answer:回答:

Go to your Python installation folder -> Lib -> site-packages -> corsheaders -> signal.py file. Go 到您的 Python 安装文件夹 -> Lib -> 站点包 -> corsheaders -> signal.py 文件。 (for me it was C:\Python310\Lib\site-packages\corsheaders\signal.py) (对我来说是 C:\Python310\Lib\site-packages\corsheaders\signal.py)

I solved the issue by changing the file to the following:我通过将文件更改为以下内容解决了这个问题:

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()

More details and smarter explanation: https://github.com/django-notifications/django-notifications/issues/322更多细节和更智能的解释: https://github.com/django-notifications/django-notifications/issues/322

The providing_args kwarg was removed in Django 4.0.在 Django 4.0 中删除了providing_args的args kwarg。

It's impossible for this to happen if you have properly installed django-cors-headers 3.10.1, since this was fixed in 3.3 .如果您正确安装了 django-cors-headers 3.10.1,这是不可能发生的,因为这已在 3.3 中修复 In addition, the line in your traceback does not exist in version 3.10.1 of the library .此外, 您的回溯中的行在 library 的 3.10.1 版本中不存在

  1. Make sure you don't have multiple different versions of the library installed, eg one within a virtualenv and one outside it (you clearly seem to have one outside a virtualenv).确保您没有安装多个不同版本的库,例如,一个在 virtualenv 内,一个在其外(显然您似乎有一个在 virtualenv 外)。
    • In fact, you seem to have two Python 3.10 installations altogether, one in AppData\Roaming\Python\Python310 and one in C:\Python310 .实际上,您似乎总共安装了两个 Python 3.10 ,一个在AppData\Roaming\Python\Python310中,一个在C:\Python310中。 You will want to clean that up first.你会想先把它清理干净。
  2. If you aren't using virtualenvs, please start using them to separate the dependencies of your different projects from one another.如果您不使用 virtualenvs,请开始使用它们来分离不同项目的依赖关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM