Local setting for INSTALLED_APPS using fabric

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

try: from local_settings import * except ImportError: pass 

trick. But I was wondering if there is a better way to uninstall the application that I want to run locally from INSTALLED_APPS in settings.py using Fabric.

+5
source share
4 answers

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

shared.py

 INSTALLED_APPS = (...) 

development.py

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

You need to make sure that during development you start the development server using the development.py settings file:

 python manage.py --settings=settings.development 

and similarly on your production server you do the same for production.py (it depends on your implementation)

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

As a third-party, instead of completely removing the compressor from installed applications, you can simply turn it on and off using the COMPRESS_ENABLED parameter

+10
source

You can also do it differently.

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

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

And here is your local_settings.py file:

 DEV_APPS = ('compressor',) 
+6
source

My approach is to rename the original settings.py (made by ./manage.py startproject ) to base_settings.py . Thus, all basic settings are in base_settings.py .

Then I will create a new settings.py file that will contain only the necessary changes and overrides for the environment from base_settings.py .

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

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

Then, any parameter necessary for the environment will be added to settings.py.

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

Helps me manage various environments.

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

+1
source

Take debugging 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 
0
source

Source: https://habr.com/ru/post/1397765/


All Articles