Hackers and Slackers

Installing Django CMS on Ubuntu

One of three major Django CMS products

Django CMS is a leading Python-based CMS.

As it stands, there is not a comprehensive guide which walks through the complete installation of Django + Django CMS. While is information exists in fragments, newcomers to Python would undoubtedly have trouble piecing much of this information together. This guide hopes to serve as a reduction of friction to Django newcomers, thus ensuring Pythonic world domination.

Stack

  • Ubuntu
  • Python 3
  • Virtualenv
  • Postgres
  • Django
  • Django CMS

Install lastest Python3 & Pip3

apt-get update
apt-get install python3 python3-pip

Create and activate a virtual environment

Virtualenv is included with Python3.

virtualenv env
source env/bin/activate

Install Django

Find out what the latest distribution is by going here.

pip3 install Django==1.11.7

Install Django CMS

pip3 install django-cms

Start Project

Create a Django project in your desired directory.

cd /home
django-admin.py startproject yourproject

Update Settings.py

We'll need to update settings.py with a few things.

cd yourproject/yourproject
vim settings.py

The resulting changes to settings.py are shown in the block below. These changes include:

  • Adding the first 4 lines in installed_apps. These apps are specific to Django CMS.
  • Adding the languages block.
  • Update the databases block with your preferred database. We'll set up the corresponding db and db user later.
  • Add site_id.
INSTALLED_APPS = [
    'django.contrib.sites',
    'cms',
    'menus',
    'treebeard',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

LANGUAGES = [
    ('en-us', 'English')
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'yourdb',
        'USER': 'youruser',
        'PASSWORD': 'yourpass123',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

SITE_ID = 1

Install Postgres

In our case, we'll be using Postgres as our db. Install Postgres along with all dependencies:

apt-get install python-dev libpq-dev postgresql postgresql-contrib 
pip3 install psycopg2

Activate the psql shell as the Postgres user:

su - postgres
psql

Create DB and DB User

Create a DB with a user, matching the information you entered in settings.py earlier:

CREATE DATABASE yourdb;
CREATE USER youruser WITH PASSWORD 'yourpass123';
ALTER ROLE youruser SET client_encoding TO 'utf8';
ALTER ROLE youruser SET default_transaction_isolation TO 'read committed';
ALTER ROLE youruser SET timezone TO 'EST';
GRANT ALL PRIVILEGES ON DATABASE yourdb TO youruser;

Exit the postgres user.

\q
exit

Migrate DB

Start the migration.

python3 manage.py migrate

Create Superuser

Create a user for the CMS.

python3 manage.py createsuperuser

Validate

Make sure everything is ok.

python3 manage.py cms check

At this point, you might notice a few errors related to sekizai. We'll need to update a few more things.

Update Settings.py (again)

We'll need to update settings.py with a few things.

Add 'sekizai' to installed_apps.

INSTALLED_APPS = [
    'django.contrib.sites',
    'cms',
    'menus',
    'treebeard',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'sekizai',
]

Add an additional 5 lines to middleware:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'cms.middleware.user.CurrentUserMiddleware',
    'cms.middleware.page.CurrentPageMiddleware',
    'cms.middleware.toolbar.ToolbarMiddleware',
    'cms.middleware.language.LanguageCookieMiddleware',
]

Add "'sekizai.context_processors.sekizai'," and "'django.template.context_processors.i18n'," to templates:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'sekizai.context_processors.sekizai',
                'cms.context_processors.cms_settings',
                'django.template.context_processors.i18n',
           ],
       },
    },
]

Finally, add your box's IP address to allowed_hosts:

ALLOWED_HOSTS = [
    '000.000.00.00',
]

Update Urls.py

Update urls.py to look like the following:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('cms.urls')),
]

Give it a go

Everything should be good! Start the app:

python3 manage.py runserver 0.0.0.0:8000

The app should now be accessible at your box's IP address.

Author image
New York City Website
Product manager turned engineer with ongoing identity crisis. Breaks everything before learning best practices. Completely normal and emotionally stable.

Product manager turned engineer with ongoing identity crisis. Breaks everything before learning best practices. Completely normal and emotionally stable.