0

The settings code is

import os
import environ

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


env = environ.Env(DEBUG=(bool, False))

environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

the allowed host is written as

ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS").split(',')

the .env file has ALLOWED_HOSTS as

ALLOWED_HOSTS=localhost 127.0.0.1 [::1]

why am I getting the fail code

AttributeError: 'NoneType' object has no attribute 'split'

when I run the command

docker-compose up --build -d --remove-orphans

2 Answers2

2

You are doing it in the wrong way -

  1. the env value should be comma-separated
    # env file
    
    ALLOWED_HOSTS=localhost,127.0.0.1,[::1]
    
  2. Access the env variable using env (instance of Env(...))
    # settings.py
    
    env = environ.Env(DEBUG=(bool, False))
    
    environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
    ALLOWED_HOSTS = env.list('ALLOWED_HOSTS')
    # or
    # ALLOWED_HOSTS = env('ALLOWED_HOSTS', cast=list)
    
JPG
  • 82,442
  • 19
  • 127
  • 206
1

I believe you're experiencing this error for two reasons:


(1) Because the ALLOWED_HOSTS inside your .env file aren't actually separated by commas. See below for current state and potential fix:

Current .env

ALLOWED_HOSTS=localhost 127.0.0.1 [::1]

Corrected .env

ALLOWED_HOSTS=localhost,127.0.0.1

(2) You are importing your ALLOWED_HOSTS strangely into settings.py, and can do so more standardly as below:

Corrected settings.py

import os
import environ

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

env = environ.Env(
    DEBUG=(bool, False),
    ALLOWED_HOSTS=(list, [])  ## Add this!
)

environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

ALLOWED_HOSTS = env.list('ALLOWED_HOSTS')  ## Change your code to this!