Brief Notes
Noteworthy links, summaries and concise prose on stuff.

Django development

All about developing with Django.

Notes

To create a new Djangoproject named djangoproject:

django-admin.py startproject djangoproject
cd djangoproject

To use an existing PostgreSQL database (for example, created earlier using CREATE DATABASE djangoproject;, change the DATABASES section in djangoproject/settings.py to:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'djangoproject',
        'HOST': 'localhost',
        'PORT': '7777',
    }
}

To set up initial database structure, in the Django project folder:

./manage.py makemigrations
./manage.py migrate
./manage.py createsuperuser

To run the development server:

./manage.py runserver 0.0.0.0:8080

To serve static files such as CSS and images for all Django apps within a Django project in Production environment with uWSGI:

Step 1: Create a static folder on the same level as djangoapps/djangoproject/settings.py, say djangoapps/djangoproject/static. This folder will act as the collection point for all static files when djangoapps/djangoproject/manage.py collectstatic is run later.

Step 2: Add the following lines in djangoapps/djangoproject/settings.py:

STATIC_ROOT = "djangoapps/static" # Be sure to specify the full path

Step 3: Execute the following in the Django project folder:

./manage.py collectstatic

Step 4: When starting uWSGI, specify the following option in the .ini file or on the commandline to let uWSGI correctly handle the serving of static files (see Serving static files with uWSGI (updated to 1.9) — uWSGI 2.0 documentation):

; uwsgi.ini file
static-map = /static=djangoapps/djangoproject/static  ; Be sure to specify the full path to the static folder

To create an additional app within a Django project:

./manage.py startapp appname

Add appname to INSTALLED_APPS section inside djangoproject/settings.py:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'appname',
)