Import custom variable inside different Environment

Hi
I have different settings file for dev, testing and production. I also have custom variables inside these settings files. (values must be changing depends on env)

How can I import/get the correct value from the settings file depends on current environment?

Thank you

Your asgi/wsgi.py file has a statement that is importing the settings module. You can alter the path in this statement to change the settings.py file that is loaded. From there you should be able to import any variables from the settings.py file into other files.

I would avoid changing that value directly in the asgi/wsgi.py files. It’s better to change the environment that controls the settings module. This topic was discussed previously on the forum at Setting the 'django_settings_module' value. Hopefully that topic provides some clear guidance.

1 Like

That’s smart. Speaking of changing the environment, do you know of a way to implement bash scripts into vscode for use with Django?

I’m sorry, I don’t know too much about VS Code. I used it briefly while playing with Rust, but I mostly stick with Vim.

Outside of code editors, you can use a tool like django-environ to read environment variables from a .env file that lives at your repository root. The nice part about this package is that it’s editor agnostic so if you work on a team, then anyone is able to handle environment variables in the same way, regardless of their editor choice.

1 Like

That’s perfect. For production I’ve been using django-decouple with an ini file but I’ll definitely look into using django-environ.

You’re a trooper for using Vim. I use it at work, but I use VSCode at home for my personal django projects.