Login

Conditional INSTALLED_APPS entry

Author:
tgandor
Posted:
May 11, 2014
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

Some INSTALLED_APPLICATIONS applications may not be critical for your website to work - for example you may only need them for development - like 'django_extensions' or 'debug_toolbar'. They needn't be installed actually for the site to work.

This way you can avoid discussions with other developers if some application should be included, or is it just spam, because if they don't like it, they don't have to install it.

On a production server you can leave this not installed, to avoid security concerns like a possibility to IP-spoof and see the debug toolbar.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# settings.py


INSTALLED_APPS = (
    'longerusername',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.admin',
    # ...
)


def _enable_conditional(application):
    global INSTALLED_APPS
    try:
        __import__(application)
        INSTALLED_APPS += (application,)
    except ImportError:
        pass

_enable_conditional('django_extensions')
_enable_conditional('debug_toolbar')

More like this

  1. FileField having auto upload_to path by junaidmgithub 14 hours, 37 minutes ago
  2. LazyPrimaryKeyRelatedField by LLyaudet 1 week, 1 day ago
  3. CacheInDictManager by LLyaudet 1 week, 1 day ago
  4. MYSQL Full Text Expression by Bidaya0 1 week, 2 days ago
  5. Custom model manager chaining (Python 3 re-write) by Spotted1270 2 weeks, 1 day ago

Comments

Please login first before commenting.