【问题标题】:Django giving error ImportError: No module named 'corsheaders'Django 给出错误 ImportError: No module named 'corsheaders'
【发布时间】:2017-03-16 05:31:09
【问题描述】:

我是 Django 的新手,正在尝试为我的服务器启用 CORS。我找不到内置的解决方案,所以我选择了django-cors-headers。我在 Ubuntu 15.04 的 python 3.5 上的 conda 环境中运行我的服务器。所以我用 pip3 install django-cors-header 安装了它。当我执行 pip3 list 时,它会安装在 1.2.2 版的 django-cors-header。在我的 django 应用程序中,我做到了。

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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    '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

但是当我运行python3 manage.py runserver 时,它不再说ImportError: No module named 'corsheaders'。 我已经阅读了所有关于它的 SO 帖子,但似乎没有一个能解决我的问题。 请帮忙

【问题讨论】:

    标签: django django-cors-headers


    【解决方案1】:

    最初我做了pip3 install django-cors-headers,而是侥幸尝试了pip install django-cors-headers,瞧,它奏效了。

    希望对某人有所帮助

    【讨论】:

    【解决方案2】:

    我想你应该输入以下命令

    pip3 install django-cors-headers

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      同样的事情发生在我身上。我只是忘了跑:

      pipenv shell
      

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题。就我而言,当 VirtualEnv 被激活时,Pip 正在将包安装到全局环境中。所以以下命令对我有用-

        <path-to-your-env>/bin/pip3 install -r requirements.txt
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-06
          • 1970-01-01
          • 2015-04-30
          • 2017-09-11
          • 1970-01-01
          • 2015-11-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多