Search code examples
pythondjangobashenvironment-variablesdevelopment-environment

How do you add environment variables to your django project


I'm trying to set up my project so that it can use environment variables locally

Ive tried adding it to the end of my activate file and a list of other things. I'm trying to use this

from .base import *

    if os.environ['DJANGO_SERVER_TYPE'] == 'local':
        try:
            from .local import *
        except:
            pass

    if os.environ['DJANGO_SERVER_TYPE'] == 'production':
        try:
            from .production import *
        except:
            pass

I am a real novice and often things are explained brief and matter of factly. So a thorough explanation would really be beneficial to me in how to implement this thanks. I've NEVER done anything n Bash. Ive tried doing this

export KEY=VALUE

in the activate file, only for it not to be recognized by the system, and I had to remove it in order to use my local server


Solution

  • If you're running it through the Django web server, you can pass environment variables the same way you would to any other command:

    DJANGO_SERVER_TYPE="local" ./manage.py runserver
    

    If you're running it through a web server like Apache, you can set environment variables through your virtual host configuration:

    SetEnv DJANGO_SERVER_TYPE local