Im trying to dockerize an existing django project.
I have corsheaders installed
and its included in my installed apps as follows:
INSTALLED_APPS = [
'corsheaders',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
and in the middleware
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
after that i build my docker using
docker-compose --build
and then run it
docker-compose up
but always the same error
ModuleNotFoundError: No module named 'corsheaders'
i have tried to delete corsheaders from my installed aps but then i get
ModuleNotFoundError: No module named 'restframework'
ps: if i run the server through py manage.py runserver
it works perfectly.
Dockerfile
# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
Requirements.txt
Django>=4.0
psycopg2>=2.8
django-cors-headers==3.10.1
djangorestframework==3.13.1
pandas
CodePudding user response:
you need to add restframework and pass the cors at the end
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
]
FROM python:3
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update
RUN apt-get install -y libpq-dev python3-dev
# install dependencies
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN mkdir /code
WORKDIR /code
CodePudding user response:
I solved it by realizing the when i install the package through :
pip install django-cors-headers
its not getting installed in the venv of the project itself. or through:
py -m pip install django-cors-headers
So i copied it to the venv and it worked.