Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django settings per application - best practice?

this is somewhat related to this question
Why is django's settings object a LazyObject?

In my django project i have several applications. Each application can have its own non-trivial settings file.

proj/     proj/          settings.py     app/          settings.py          views.py 

What is the general best practice here?
should app/settings.py do

from django.conf import settings APP_SETTING= lambda: settings.getattr('APP_SETTING', 'custom_value') PROJ_SETTING= lambda: settings.PROJ_SETTING 

and then in app/views.py do

import .settings  X = settings.APP_SETTING Y = settings.PROJ_SETTING 

or should I be modifying the django lazy settings object in app/settings.py as per the django coding style?

from django.conf import settings # not even sure how I would check for a default value that was specified in proj/settings.py settings.configure(APP_SETTING='custom_value') 

and then each app/views.py just consumes proj/settings.py via django.conf settings?

from django.conf import settings X = settings.APP_SETTING Y = settings.PROJ_SETTING 

There are obviously quite a few other permutations but I think my intent is clear.
Thanks in advance.

like image 375
w-- Avatar asked Mar 13 '14 17:03

w--


People also ask

Should Django app names be singular or plural?

App naming conventions An app's name should follow Pep 8 Guidelines, namely it should be short, all-lowercase and not include numbers, dashes, periods, spaces, or special characters. It also, in general, should be the plural of an app's main model, so our posts app would have a main model called Post .

How do I organize my Django project?

The way I like to organize my Django Project is – Keeps all Django apps in apps folder, static files (scripts, js, CSS) in the static folder, HTML files in templates folder and images and media content in the media folder.

What is the typical function of settings py in Django web development?

settings.py contains all the website settings, including registering any applications we create, the location of our static files, database configuration details, etc.


1 Answers

The simplest solution is to use the getattr(settings, 'MY_SETTING', 'my_default') trick that you mention youself. It can become a bit tedious to have to do this in multiple places, though.

Extra recommendation: use a per-app prefix like MYAPP_MY_SETTING.

There is a django app, however, that gets rid of the getattr and that handles the prefix for you. See http://django-appconf.readthedocs.org/en/latest/

Normally you create a conf.py per app with contents like this:

from django.conf import settings from appconf import AppConf  class MyAppConf(AppConf):     SETTING_1 = "one"     SETTING_2 = (         "two",     ) 

And in your code:

from myapp.conf import settings  def my_view(request):     return settings.MYAPP_SETTINGS_1  # Note the handy prefix 

Should you need to customize the setting in your site, a regular entry in your site's settings.py is all you need to do:

MYAPP_SETTINGS_1 = "four, four I say" 
like image 148
Reinout van Rees Avatar answered Oct 03 '22 13:10

Reinout van Rees