How to fix AttributeError: 'NoneType' object has no attribute 'encode' in setting.py Django
I want to streamline the coding process by running DATABASE_URL from docker-compose files to use setting.py, can you suggest a solution to the error I'm experiencing?
When I use python manage.py migrate I came across this problem. DATABASES = {'default': dj_database_url.parse(DATABASE_URL.encode())} AttributeError: 'NoneType' object has no attribute 'encode'
docker-compose.yml
version: '3.7'
services:
db:
image: mariadb:10
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=mariadb
- MYSQL_DATABASE=mariadb
- MYSQL_USER=mariadb
- MYSQL_PASSWORD=mariadb
- MARIADB_ROOT_PASSWORD=mysecretpassword
ports:
- 3306:3306
volumes:
- "mysqldata:/var/lib/mysql"
web:
build: .
restart: always
command: python manage.py runserver 0.0.0.0:8000
environment:
- DATABASE_URL=mariadb+mariadbconnector://user:mariadb@db:3306/mariadb
ports:
- "8000:8000"
depends_on:
- db
volumes:
mysqldata:
setting.py
import os
import dj_database_url
DATABASE_URL = os.environ.get('DATABASE_URL')
DATABASES = {'default': dj_database_url.parse(DATABASE_URL.encode())}
The variable DATABASE_URL
seems to be None
, you could exec into the docker container and printenv
in order to see the environment variables. (docker exec <container_name> printenv