【问题标题】:No 'Access-Control-Allow-Origin' using django rest framework api没有使用 django rest framework api 的“Access-Control-Allow-Origin”
【发布时间】:2018-09-06 06:34:03
【问题描述】:
pip3 install django-cors-headers
Requirement already satisfied: django-cors-headers in /usr/local/lib/python3.6/site-packages
You are using pip version 9.0.1, however version 9.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

我运行了以下命令:

然后我将其分别设置在我的 INSTALLED_APPS 中,如下所示:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders'
]

然后我像这样在中间件中使用它,

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware'
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

我收到以下错误:

ModuleNotFoundError: No module named 'corsheaders.middleware.CorsMiddlewaredjango'; 'corsheaders.middleware' is not a package

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 140, in inner_run
    handler = self.get_handler(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
    handler = super().get_handler(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 65, in get_handler
    return get_internal_wsgi_application()
  File "/usr/local/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 49, in get_internal_wsgi_application
    ) from err
django.core.exceptions.ImproperlyConfigured: WSGI application 'instawork.wsgi.application' could not be loaded; Error importing module.

我遵循了严格的指令,我目前也在使用最新的 django 框架。

【问题讨论】:

    标签: django


    【解决方案1】:

    你错过了逗号,正确的MIDDLEWARE 是:

    MIDDLEWARE = [
        'corsheaders.middleware.CorsMiddleware',
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]
    

    CORS_ORIGIN_ALLOW_ALL=True 放在INSTALLED_APPS 之前。您还可以指定 cors 允许的主机:

    CORS_ORIGIN_WHITELIST = (
        'example.com',
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 2018-03-28
      • 2016-05-13
      • 2014-04-16
      • 2017-06-15
      • 2018-12-12
      • 2019-01-05
      相关资源
      最近更新 更多