📜 ⬆️ ⬇️

Beautiful Django configs

During that time, while I was engaged in the implementation of projects written in Django, I accumulated simple techniques that simplify the deployment. Let's look at settings.py, which was used in one of the latest projects. Fully available at http://gist.github.com/214361
Let's start.

Local settings:

Local settings allow you to have different configurations of the same project on different machines. At the end of my every settings.py file there is a local_settings.py connection. This allows you to override any variable values ​​from settings.py. For example, on the production server I use mysql, and on the local machine - sqllite.
try :
from local_settings import *
except ImportError :
pass


Debug flag:


Instead of manually changing DEBUG = True / False, we define the server on which Django is running and change the flag automatically. If there are several sites (test server, QA server), we simply add domains to the check.
if socket . gethostname() == 'your.domain.com' :
DEBUG = False
else :
DEBUG = True

')
Path to media files:

PROJECT_PATH = os . path . realpath(os . path . dirname(__file__))
MEDIA_ROOT = os . path . join(PROJECT_PATH, 'media' )

The first line will create a variable that stores the path to the settings.py file, which I store in the root of the project directory; the media folder is also located there. Thus, you can automatically build a full path to media, no matter where the project is deployed. PROJECT_PATH is also useful for setting up third-party applications.

We use only what we need:

if DEBUG:
TEMPLATE_CONTEXT_PROCESSORS += ( 'django.core.context_processors.debug' ,)
if USE_I18N:
TEMPLATE_CONTEXT_PROCESSORS += ( 'django.core.context_processors.i18n' ,)
if DEBUG:
MIDDLEWARE_CLASSES += ( 'debug_toolbar.middleware.DebugToolbarMiddleware' ,)

Maybe I'm a little meticulous, but I love when only what is actually used is included in the project. Therefore, we discard the excess using flags.

Paths to patterns:

# Dir Structure
# + Application
# + templates
# + Application
# - someTemplate.html
# - models.py
# - views.py
# - otherAppSpecificFiles.py
# + OtherApplication
# + Templates
# - base.html
# - settings.py
# - urls.py
# - otherfiles.py
TEMPLATE_DIRS = ()
for root, dirs, files in os . walk(PROJECT_PATH)
if 'templates' in dirs: TEMPLATE_DIRS += (os . path . join(root, 'templates' ),)


I store the templates folder in the application root, so you can use a simple crawl to get full paths to the templates.

Database connection settings:

# The database settings are left blank so to force the use of local_settings.py below
DATABASE_ENGINE = ''
DATABASE_NAME = ''
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''


I always leave the database settings empty, it forces you to include them in the local_settings.py local settings. I use sqllite for development and mysql (or postgresql) for production, but due to the fact that the settings are stored in local_settings.py, you can safely commit and update settings.py

I apply the techniques in my own experience - it helps. Have a nice deployment!

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


All Articles