abhinavkulkarni
abhinavkulkarni

Reputation: 2389

App level settings in Django

What is the convention about including app-level settings in Django? For e.g. I am writing a Django app that has a view that makes an API call to another webservice, gets its response, modifies it and returns it back. Now, I would like to have a settings variable that will hold the value of this API URL (and depending upon whether Django web server is running in stage/dev/prod, the API URL will differ). Should I create a settings.py in app directory? Or modify the project's settings.py? If I do later, then app no longer remains pluggable/portable. Is that okay?

Please provide a rationale for you response (i.e. please explain why it may be a good idea to include the setting in global settings.py even though it reduces the portability of the app).

Thanks.

Upvotes: 3

Views: 1279

Answers (1)

michaelb
michaelb

Reputation: 747

Make a global settings.py, and specify which settings are needed in the apps documentation. You should not make app-specific settings.py files, unless they have some unrelated, internal use. You might want to use something like getattr(settings, 'SOME_SETTING_NAME', 'default value') to fetch the option with a default.

If you want to have separate settings for prod/staging/dev, then you'll want to make a settings/ folder with an __init__.py file that imports based on an ENV variable.

Upvotes: 4

Related Questions