Search code examples
djangodotenv

Dotenv Doesn't Handle Multiple Hosts in ALLOWED_HOSTS


In my production setting.py file I have:

from dotenv import load_dotenv
load_dotenv(override=True)

DEBUG = os.getenv('DEBUG')

#ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS")
ALLOWED_HOSTS = ['example.com', 'www.example.com']

In my .env file in production, I have

DEBUG=False
ALLOWED_HOSTS=['www.example.com', 'example.com']

If I switch the commented out line in settings, I get an error saying url may not be in the allowed hosts. I have a number of other definitions in the .env file that work fine although I'm not sure about debug. I've tried all sorts of combinations on the ALLOWED_HOSTS and get the same error. In development I have:

ALLOWED_HOSTS='localhost'

That setting works fine. Any idea what I'm doing wrong?


Solution

  • I found a portion of answer here. Then I did that :

    #setting.py
    ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS").split(' ') 
    
    #.env
    ALLOWED_HOSTS = domain1 domain2 domain3 domain4
    

    And it works.