10

I am working on a django app that needs a directory to download and store files.

I want to keep my app reusable so I do not want to hard code the path of this directory. So I want to make this path a setting/a global variable that can be set up.

Where could I put this setting/global variable?

Is this kind of approach good ? http://blog.muhuk.com/2010/01/26/developing-reusable-django-apps-app-settings.html

Thanks for your advice!

Michael
  • 8,357
  • 20
  • 58
  • 86
  • Welcome to Stack Overflow! Great first question. – Paul D. Waite Feb 25 '12 at 19:00
  • Broken link at muhuk.com. – Paul Draper Mar 12 '13 at 10:26
  • link still doesn't work, but google's cache helps out: http://webcache.googleusercontent.com/search?q=cache:mNs3to1345cJ:blog.muhuk.com/2010/01/26/developing-reusable-django-apps-app-settings.html+&cd=1&hl=en&ct=clnk&gl=de#.UntjOmTuJII – linqu Nov 07 '13 at 09:58
  • This is the [new location](http://blog.muhuk.com/2010/01/26/developing-reusable-django-apps-app-settings.html) of the link. – rwx Feb 20 '16 at 17:35

1 Answers1

12

I use the following methodology:

# some file in your app:

from django.conf import settings

MY_APP_SETTING = getattr(settings, 'MY_APP_SETTING', 'some default value')

This effectively allows end-users to customized the setting in their own settings.py, but still ensures that there's always some default value set. You can now use MY_APP_SETTING at will in the rest of your code.

UPDATE

The link in your question was taking too long to load, so I just went ahead and answered. As it turns out, the method I suggested is the same as what it suggests, so yes, I'd consider that approach good ;).

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Sad part is that it won't make sense if the said setting can't have any default value (an API key, for instance) – Anto Apr 21 '14 at 20:13