Will & Skill Developers

Will & Skill Developers


Thoughts, snippets and ideas from the team at Will & Skill AB, Stockholm.

Faisal Mahmud
Author

“The mind is not a vessel to be filled, but a fire to be kindled.” ― Plutarch

Share


Tips for a cleaner Django settings.py

Faisal MahmudFaisal Mahmud

Clean paths

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  
# The root of the git repo - Could be ~/project or ~/repo
REPO_DIR = os.path.realpath(os.path.join(BASE_DIR, '..'))  
# The directory of the current user ie /home/django a.k.a. ~
HOME_DIR = os.path.realpath(os.path.join(REPO_DIR, '..'))  
# The directory where collectstatic command copies/symlinks the files to
# This can/should be located at ~/staticfiles, preferrably outside the git repo
STATIC_DIR = os.path.realpath(os.path.join(HOME_DIR, 'staticfiles'))  
# The directory where different applications uploads media files to
# This can/should be located at ~/media, preferrably outside the git repo
MEDIA_DIR = os.path.realpath(os.path.join(HOME_DIR, 'media'))

MEDIA_ROOT = MEDIA_DIR

INSTALLED_APPS

  1. Separate the INSTALLED_APPS list into smaller lists
  2. State the modules in alphabetical order if possible
DJANGO_APPS = [  
    'djangocms_admin_style',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.staticfiles',
]

DJANGO_CMS_APPS = [  
    'cms',
    'easy_thumbnails',
    'filer',
    'menus',
    'mptt',
    'sekizai',
    'treebeard',
]

THIRD_PARTY_APPS = [  
    'rest_framework',
    'reversion',
]

PROJECT_APPS = [  
    'core',
    'cmsplugin',
    'posts',
    'search',
    'staff',
]

INSTALLED_APPS = DJANGO_APPS + DJANGO_CMS_APPS + THIRD_PARTY_APPS + PROJECT_APPS  

INTERNAL_IPS

It is smart to use the INTERNAL_IPS variable in case You are developing on a remote server to avoid unwanted access to Your development enviroment!

INTERNAL_IPS = [  
    '123.456.789.123',
]

Use an in memory database for testing if project is small

This will work until Your database is too big. At that point it is much faster to run tests on Postgres for instance.

if 'test' in sys.argv:  
    DATABASES['default'] = {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}

Use an assets folder to store static files such as javascript (.js) and stylesheets (.css)

STATICFILES_DIRS = [  
    os.path.join(REPO_DIR, "assets"),
]

Output email content to the console in Your test environment

# EMAIL SETTINGS
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'  

Be careful not to expose sensitive data in your settings.py

NOTE: Do not commit sensitive data to Your Git repo! It is much smarter to store it on the server! Read more at https://12factor.net/

Stuff that is often sensitive

If You already have committed sensitive data to Your repo

You can use git filter-branch like so

$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' --prune-empty --tag-name-filter cat -- --all

Follow the steps below to remove sensitive data from your Git repo

  1. Copy your sensitive file that contains sensitive data
  2. Replace the data by using something like os.environ['YOUR_VARNAME'] or os.environ.get('YOUR_VARNAME', 'DEFAULT_VALUE')
  3. Run the command git filter-branch described above or read more at https://help.github.com/articles/removing-sensitive-data-from-a-repository/
  4. Commit the cleaned file to Your git repo
  5. Push to your remote github/bitbucket repo
Faisal Mahmud
Author

Faisal Mahmud

“The mind is not a vessel to be filled, but a fire to be kindled.” ― Plutarch

Comments