Brainstorm's snippets (1/234)

  New Django project check-list

A check list of what I normally do when creating a brand new Django project.

1   Initial project skeleton

  • create a local virtual environment for development, and install lastest version of django in it

  • create a folder container for the project as follows:

    |- customerName
         |- myNewProjectName
               |- logs
               |- dumps
               |- public
               |     |- media
               |     |- static
               |- myNewProjectName  <-- project sources go here
    
  • create the project skeleton with python django-admin startproject and name it main

  • replace settings.py file with a "settings" module, and adjust it as needed; the final project layout is:

    |- customerName
         |- myNewProjectName
               |- logs
               |- dumps
               |- public
               |     |- media
               |     |- static
               |- myNewProjectName
                     |- manage.py
                     |- main
                           |- settings
                               |- __init__.py
                               |- debug.py
                               |- local.py
                               |- settings.py
                               |- test_settings.py
                               |- test_settings_no_migrations.py
    

    where:

    main/settings/__init__.py:
    
        from main.settings.local import *
    
    main/settings/local.py:
    
        from main.settings.settings import *
        ...
    
    main/settings/debug.py:
    
        from main.settings.local import *
        DEBUG = True
    
  • main/settings/local.py will contain db settings and the secret_key, and will be excluded from GIT repo

  • add a requirements file to keep track of installed modules and versions

3   Custom User model

  • create a "users" app
  • use it to override User model (derived from django.contrib.auth.models.AbstractUser)
  • add and configure Hijack app

4   Versioning and other initializations

  • create a git repo for the project
  • create an "admin_ex" app for a customized (possibly empty) AdminSiteEx(admin.AdminSite)
  • add a main.apps.MainConfig to install admin_ex.admin_site.AdminSiteEx as project admin
  • define admin.site.site_header and admin.site.site_title variables
  • configure bumpversion to manage versioning

5   Deployment procedure

  • add "django-channels" to have daphne installed
  • setup a provisioning procedure for initial host setup
  • setup a deployment procedure for project installation and update
  • run it over a staging server asap

6   Email setting

8   Add helper apps

  • django-constance
  • django-smuggler
  • django-debug-toolbar
  • django-compressor

9   Review settings for security

  • python manage.py check --deploy

10   Sample README.rst file

Project "XXXXX"
===============

Manual provisioning on local development machine
------------------------------------------------

Create environment for new project::

    $ cd /Projects/
    $ mkdir XXXXX
    $ mkdir XXXXX/public
    $ mkdir XXXXX/public/static
    $ mkdir XXXXX/public/media

Create and activate a virtualenv for the project::

    $ mkvirtualenv XXXXX
    $ workon XXXXX

Clone source repository::

    $ cd /Projects/XXXXX
    $ git clone git@gitlab.brainstorm.it:YYYYY/XXXXX.git

The final project layout is::

    .
    ├── XXXXX
    │   ├── README.rst
    │   ├── ...
    │   ├── manage.py
    │   ├── main          <---- this is the Django project
    │   ├── requirements
    │   └── ...
    └── public
        ├── media
        └── static

Update the virtualenv (that is: Install required python packages)::

    $ cd /Project/XXXXX/XXXXX
    $ pip install -r requirements/development.txt

Create a local settings file;
for example, copy and adapt "local_example.py"::

    $ cd /Project/XXXXX/XXXXX
    $ cp main/settings/local_example.py main/settings/local.py

Create database (use database name and password specified in local.py)::

    $ psql
    $ create user XXXXX with encrypted password '<PASSWORD>';
    $ create database XXXXX owner XXXXX;

Populate database struct::

    $ python manage.py migrate

Update front-end assets (if any)::

    $ npm install

Create a supersuser account::

    $ python manage.py createsuperuser

Run the development web server::

    $ python manage.py runserver 0.0.0.0:8000


Development workflow
--------------------

::

    $ cd /Project/XXXXX/XXXXX
    $ workon XXXXX
    $ git pull
    $ git submodule update
    $ pip install -r requirements/development.txt
    $ python manage.py migrate
    $ npm install
    $ python manage.py runserver

11   Monitoring