Gunicorn not using right settings file Django?

In my wsgi.py I am conditionally setting DJANGO_SETTINGS_MODULE to two different files(local and production).
On the server I have set "PROD" variable in /etc/profile

if "PROD" in os.environ:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
else:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings_dev")

But, still I am getting an error because right settings file is not being set. So maybe if condition isn't working. See below pic.

enter image description here

My gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/myproject/myproject
ExecStart=/usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/myproject/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

1 answer

  • answered 2018-01-14 11:28 illagrenan

    You should set PROD environment variable in your Gunicorn configuration this way:

    [Service]
    environment=PROD=True
    User=root
    Group=www-data
    WorkingDirectory=/home/myproject/myproject
    ExecStart=/usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/myproject/myproject/myproject.sock myproject.wsgi:application
    

    // EDIT: I overlooked that you set ENV var in /etc/profile... You can try my solution, otherwise I will delete my answer.