Loading

Deploying Django

by drk last updated on 2015/07/28 ( django / python / programming )


Deploying a django project tips. Be sure to read the official documentation as well.

Settings

When deploying a website, there's always the issue on how to deal with the differences between the development version, and the live version. Here's some ideas on how to do that.

To know if its the development or live environment, we'll always check the DEBUG setting. To change this setting, we'll use an environment variable.

# settings.py

import os

    # when specifying a path, always construct it from the 'BASE_DIR'
    # os.path.join( BASE_DIR, 'path' )
BASE_DIR = os.path.dirname( os.path.dirname( os.path.abspath( __file__ ) ) )


    # in the live server, you'll need to set an environment variable 'LIVE'
    # from this point on, just read this variable to deal with
    # the differences between development/live
if os.environ.get( 'LIVE' ):
    DEBUG = False

else:
    DEBUG = True


    # make sure the secret key is a large random value,
    # and that its private
if DEBUG:
    SECRET_KEY = 'hai'

else:
    SECRET_KEY = os.environ[ 'SECRET_KEY' ]


    # need to specify the allowed hosts for the live version
ALLOWED_HOSTS = [ '.website.pt' ]


    # make sure all the static/templates/etc files
    # are constructed from the 'BASE_DIR'
STATICFILES_DIRS = [
    os.path.join( BASE_DIR, 'static/' ),
]

    # can also have some development only paths, for example
if DEBUG:
    STATICFILES_DIRS.append( 'dev_static/' )


    # don't forget to set the 'STATIC_ROOT'
    # when you run the 'collectstatic' command later on,
    # this is where the files will be copied to
STATIC_ROOT = os.path.join( BASE_DIR, 'static_root' )


# -------------------------------------------------


# views.py

from django.conf import settings
from django.shortcuts import render


def home( request ):

    context = {}

        # to distinguish between development/live in a view
    if settings.DEBUG:
        context[ 'where' ] = 'development'

    else:
        context[ 'where' ] = 'live'

    return render( request, 'home.html', context )

Commands

After you have your code on the server, there's some commands that need to be done.

Set the environment variables.

printenv           # print all the variables
printenv VARIABLE  # print the value of the specified environment variable
export VARIABLE='value'  # add an environment variable

    # so, for our case
export LIVE='True'  # doesn't really matter the value, just that it is there
export SECRET_KEY='random_key'  # again, make sure its a large random value,
                                # and don't share it with no one

There's also some django commands needed.

    # will initialize the database (apply migrations)
    # make sure you have the migrations done
    # (with the 'python manage.py makemigrations appname' command)
python manage.py migrate

    # create a super user, if needed
python manage.py createsuperuser

    # copy all the static files to the 'STATIC_ROOT' directory
    # easier to serve static files from a single place
python manage.py collectstatic

    # check for some issues it may have
python manage.py check --deploy
back

Categories