Local setting for INSTALLED_APPS using Fabric
Asked Answered
P

4

5

I have an app(django-compressor) that I only want to run on my local machine and not the server. I know about the

try:
    from local_settings import *
except ImportError:
    pass 

trick. But I was wondering if there was a better way to remove the app I only want run locally from the INSTALLED_APPS in the settings.py using Fabric.

Pella answered 22/2, 2012 at 9:54 Comment(0)
R
10

I think the standard approach you mentioned is best; create a folder settings with three settings files; shared.py, production.py and development.py. Settings that are common to all instances of your app are placed in shared.py and this is imported from production.py and development.py. Then you can easily only add compressor in your development settings

shared.py

INSTALLED_APPS = (...)

development.py

from settings.shared import *
INSTALLED_APPS += ('compressor',)

You need to make sure then when developing, you run the development server with the development.py settings file:

python manage.py --settings=settings.development 

and similarly on your production server you do the same for production.py (this is down to your implementation)

This is a much better approach in the long term as you can also specify separate cache, database, search etc. settings too.

As an aside, instead of completely removing compressor from your installed apps, you can simply enable and disable is using it's COMPRESS_ENABLED setting

Rarefied answered 22/2, 2012 at 10:28 Comment(4)
thanks for the great answer. Regarding the COMPRESSOR_ENABLED setting, if I set it to FALSE I am still left with templates that have the {% compress js %} and {% endcompress %} which then break on the server. Is there a way to make django-compressor remake the template files without these tags? This question I made is about this #9393493 Thanks for the help on this question and any help on the other questions.Pella
I'm not sure I understand. When COMPRESSOR_ENABLED = False (but still in the INSTALLED_APPS), those blocks shouldn't cause any error - they should just be ignored.Centralia
Even with COMPRESS_ENABLED = False I get TemplateSyntaxError at / ... Caught FilterError while rendering: /bin/sh: coffee: not foundPella
I found this comment #8900413 "With COMPRESS_ENABLED=False. The only thing that happens are the commands in COMPRESS_PRECOMPILERS." I have COMPRESS_PRECOMPILERS = (('text/coffeescript', 'coffee --compile --stdio'),) The issue is that the server tries this and has nothing called coffee. Hmmmm.Pella
G
6

You can also do it in another way.

All the shared settings are in settings.py and keep the difference in local_settings. In your case it is INSTALLED_APPS, you can change your import section to something like this:

DEV_APPS = None
try:
    from local_settings import *
    INSTALLED_APPS += DEV_APPS
except:
    PASS

And here is your local_settings.py:

DEV_APPS = ('compressor',)
Gormless answered 21/5, 2013 at 10:19 Comment(1)
Simple and very straight forward. Just one question: wouldn't it be better to set DEV_APPS = (). Then you cocatenate two tuples instead of adding None and tupple.Boonie
J
1

My approach is to rename the original settings.py (made by ./manage.py startproject) into base_settings.py. This way all the baseline settings are in base_settings.py.

I will then create a new settings.py file that will only contain the needed environment-specific modifications and overrides from base_settings.py.

So, to answer your question using my approach, my settings.py will be like this:

from .base_settings import *

INSTALLED_APPS += ('compressor',)

Any necessary environment-specific setting will then be added in settings.py.

With this approach, I don't need to specify the --settings parameter when calling ./manage.py or to set the DJANGO_SETTINGS_MODULE.

Makes managing different environments much easier for me.

Note: I use git and I add settings.py to .gitignore for this approach.

Johny answered 8/4, 2015 at 7:18 Comment(0)
F
0

Take add debug tools for example . Django==2.1.7

# proj/settings/defaults.py
....

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    # apps
    'apps.document.apps.DocumentConfig',
]
# proj/settings/dev.py

DEBUG = True
from .defaults import *

INSTALLED_APPS += ['debug_toolbar']
# proj/urls.py
if settings.DEBUG:
    import debug_toolbar

    urlpatterns = [
                      path('__debug__/', include(debug_toolbar.urls)),
                  ] + urlpatterns

./manage.py runserver --settings=proj.settings.dev
Fireproof answered 20/8, 2019 at 23:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.