Search code examples
pythondjangodockercsrf

Django CSRF failing with .env file for Docker


Hi I'm using Docker to build a Django web application via Docker-Compose and it uses the .env.dev file for the environmental variables.

The problem is the CSRF_TRUSTED_ORIGINS variables :

  • If i put variables inside the settings.py => Ok
  • If i put variables inside the env.dev => KO ( AttributeError: 'NoneType' object has no attribute 'split')

settings.py

# CSRF_TRUSTED_ORIGINS=["https://www.site1.fr", "http://www.site1.fr"]
CSRF_TRUSTED_ORIGINS= os.environ.get("DJANGO_CSRF").split(" ")

env.dev

DJANGO_CSRF=https://www.site1.fr http://www.site1.fr

Solution

  • Try to name you file dev.env or just .env.

    After renaming your file, remember to use docker-compose down and docker-compose up so the environment variables get updated with the container