Installing django CMS by hand

This is how to install django CMS ‘the hard way’ (it’s not really that hard, but there is an easier way).

It’s suitable if you want to dive in to integrating django CMS into an existing project, are already experienced at setting up Django projects or indeed like to do things the hard way.

If you prefer an easier way using an automated configuration tool - definitely recommended for new users - see Installing django CMS, which is part of a complete introductory tutorial.

This document assumes you are familiar with Python and Django. After you’ve integrated django CMS into your project, you should be able to follow the Tutorials.

Requirements

Note

When installing the django CMS using pip, all of the dependencies will be installed automatically.

These packages are not required, but they provide useful functionality with minimal additional configuration and are well-proven.

Text Editors

Other Plugins

  • djangocms-link
  • djangocms-snippet
  • djangocms-style
  • djangocms-column
  • djangocms-grid
  • djangocms-oembed
  • djangocms-table

File and image handling

Revision management

  • django-reversion 1.8.X (with Django 1.6.X and Django 1.7.X) to support versioning of your content.

    Note

    As of django CMS 3.0.x, only the most recent 10 published revisions are saved. You can change this behaviour if required with CMS_MAX_PAGE_PUBLISH_REVERSIONS. Be aware that saved revisions will cause your database size to increase.

Installing

Installing in a virtualenv using pip

Installing inside a virtualenv is the preferred way to install any Django installation.

  1. sudo pip install --upgrade virtualenv
  2. virtualenv env

Note

If you are not using a system-wide install of Python (such as with Homebrew), omit the usage of sudo when installing via pip.

Switch to the virtualenv at the command line by typing:

  1. source env/bin/activate

Next, install the CMS:

  1. pip install django-cms

This will automatically install all of the requirements listed above.

While you could install packages one at a time using pip, we recommend using a requirements.txt file. The following is an example requirements.txt file that can be used with pip to install django CMS and its dependencies:

  1. # Bare minimum
  2. django-cms>=3.0
  3. # These dependencies are brought in by django CMS, but if you want to
  4. # lock-in their version, specify them
  5. Django>=1.7
  6. South==1.0.2 # Only needed for Django < 1.7
  7. django-mptt==0.6.1
  8. django-sekizai==0.7
  9. django-classy-tags==0.5
  10. djangocms-admin-style==0.2.2
  11. html5lib==0.999
  12. six==1.3.0
  13. # Optional, recommended packages
  14. Pillow>=2
  15. django-filer==0.9.8
  16. cmsplugin-filer==0.10.1
  17. django-reversion==1.8

Note

In the above list, packages are pinned to specific version as an example; those are not mandatory versions; refer to requirements for any version-specific restrictions.

If you are using PostgreSQL as your database, add the Python adapter to your requirements file:

  1. psycopg2

For MySQL you would instead add:

  1. mysql-python

Note

While the django CMS is compatible with Python 3.3+, the mysql-python package is not.

Before you install the Python adapters for your chosen database, you will need to first install the appropriate headers and development libraries. See the platform specific notes below.

Installing on Ubuntu

If you’re using Ubuntu (tested with 14.04), the following should get you started:

  1. sudo aptitude install python-pip
  2. sudo pip install virtualenv

Next, install the appropriate libraries to build the Python adapters for your selected database. For PostgreSQL:

  1. sudo aptitude install libpq-dev postgresql-client-9.3 python-dev

For MySQL:

  1. sudo aptitude install libmysqlclient-dev python-dev

Installing and configuring database servers are beyond the scope of this document. See Databases below for more information and related links.

Installing on Mac OSX

If you are using the system provided Python (2.6 or later), ensure you have pip installed.

  1. sudo easy_install pip
  2. sudo pip install virtualenv

If you’re using Homebrew you can install pip and virtualenv with the python generic package:

  1. brew install python
  2. pip install virtualenv

Next, install the appropriate libraries to build the Python adapters for your selected database. For PostgreSQL:

  1. brew install postgres

For MySQL:

  1. brew install mysql

Note

Homebrew does not set the databases to run automatically. The software necessary for the Python adapters will be installed but if you wish to run the database server locally, follow the Homebrew instructions shown in the terminal output after installing.

Databases

We recommend using PostgreSQL or MySQL with django CMS. Installing and maintaining database systems is outside the scope of this documentation, but is very well documented on the systems’ respective websites.

To use django CMS efficiently, we recommend:

  • Creating a separate set of credentials for django CMS.
  • Creating a separate database for django CMS to use.

Configuration and setup

Preparing the environment

The following steps assume your Django project will be - or already is - in ~/workspace/myproject, and that you’ll be using a virtualenv.

If you already have a virtualenv with a project in it, activate it and move on to Configuring your project for django CMS.

Otherwise:

  1. cd ~/workspace/myproject/
  2. virtualenv env
  3. source env/bin/activate
  4. pip install -r requirements.txt

Create a new Django project

  1. django-admin.py startproject myproject

If this is new to you, you ought to read the official Django tutorial, which covers starting a new project.

Configuring your project for django CMS

Open the settings.py file in your project.

To make your life easier, add the following at the top of the file:

  1. # -*- coding: utf-8 -*-
  2. import os
  3. gettext = lambda s: s
  4. BASE_DIR = os.path.dirname(os.path.dirname(__file__))

Add the following apps to your INSTALLED_APPS. This includes django CMS itself as well as its dependencies and other highly recommended applications/libraries:

  1. 'cms', # django CMS itself
  2. 'mptt', # utilities for implementing a tree
  3. 'menus', # helper for model independent hierarchical website navigation
  4. 'south', # Only needed for Django < 1.7
  5. 'sekizai', # for javascript and css management
  6. 'djangocms_admin_style', # for the admin skin. You **must** add 'djangocms_admin_style' in the list **before** 'django.contrib.admin'.
  7. 'django.contrib.messages', # to enable messages framework (see :ref:`Enable messages <enable-messages>`)

Also add any (or all) of the following plugins, depending on your needs (see the note in The INSTALLED_APPS setting about ordering):

  1. 'djangocms_file',
  2. 'djangocms_flash',
  3. 'djangocms_googlemap',
  4. 'djangocms_inherit',
  5. 'djangocms_picture',
  6. 'djangocms_teaser',
  7. 'djangocms_video',
  8. 'djangocms_link',
  9. 'djangocms_snippet',

Note

Most of the above plugins were previously distributed with django CMS, however, most of them are now located in their own repositories and renamed. Furthermore plugins: 'cms.plugins.text' and 'cms.plugins.twitter' have been removed from the django CMS bundle. Read 3.0 release notes for detailed information.

Warning

Adding the 'djangocms_snippet' plugin is a potential security hazard. For more information, refer to snippet_plugin.

Some commonly-used plugins are described in more detail in Some commonly-used plugins. There are even more plugins available on the django CMS extensions page.

In addition, make sure you uncomment (enable) 'django.contrib.admin'

You may also wish to use django-filer and its components with the django CMS plugin instead of the djangocms_file, djangocms_picture, djangocms_teaser and djangocms_video core plugins. In this case you should check the django-filer documentation and django CMS plugin documentation for detailed installation information, and then return to this tutorial.

If you opt for the core plugins you should take care that directory to which the CMS_PAGE_MEDIA_PATH setting points (by default cms_page_media/ relative to MEDIA_ROOT) is writable by the user under which Django will be running. If you have opted for django-filer there is a similar requirement for its configuration.

If you want versioning of your content you should also install django-reversion and add it to INSTALLED_APPS:

  • 'reversion'

You need to add the django CMS middlewares to your MIDDLEWARE_CLASSES at the right position:

  1. MIDDLEWARE_CLASSES = (
  2. 'django.contrib.sessions.middleware.SessionMiddleware',
  3. 'django.middleware.csrf.CsrfViewMiddleware',
  4. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  5. 'django.contrib.messages.middleware.MessageMiddleware',
  6. 'django.middleware.locale.LocaleMiddleware',
  7. 'django.middleware.doc.XViewMiddleware',
  8. 'django.middleware.common.CommonMiddleware',
  9. 'cms.middleware.user.CurrentUserMiddleware',
  10. 'cms.middleware.page.CurrentPageMiddleware',
  11. 'cms.middleware.toolbar.ToolbarMiddleware',
  12. 'cms.middleware.language.LanguageCookieMiddleware',
  13. )

You need at least the following TEMPLATE_CONTEXT_PROCESSORS:

  1. TEMPLATE_CONTEXT_PROCESSORS = (
  2. 'django.contrib.auth.context_processors.auth',
  3. 'django.contrib.messages.context_processors.messages',
  4. 'django.core.context_processors.i18n',
  5. 'django.core.context_processors.request',
  6. 'django.core.context_processors.media',
  7. 'django.core.context_processors.static',
  8. 'sekizai.context_processors.sekizai',
  9. 'cms.context_processors.cms_settings',
  10. )

Note

This setting will be missing from automatically generated Django settings files, so you will have to add it.

Warning

Be sure to have 'django.contrib.sites' in INSTALLED_APPS and set SITE_ID parameter in your settings: they may be missing from the settings file generated by django-admin depending on your Django version and project template.

Changed in version 3.0.0.

Warning

Django messages framework is now required for the toolbar to work properly.

To enable it you must be check the following settings:

  • INSTALLED_APPS: must contain 'django.contrib.messages'
  • MIDDLEWARE_CLASSES: must contain 'django.contrib.messages.middleware.MessageMiddleware'
  • TEMPLATE_CONTEXT_PROCESSORS: must contain 'django.contrib.messages.context_processors.messages'

Point your STATIC_ROOT to where the static files should live (that is, your images, CSS files, Javascript files, etc.):

  1. STATIC_ROOT = os.path.join(BASE_DIR, "static")
  2. STATIC_URL = "/static/"

For uploaded files, you will need to set up the MEDIA_ROOT setting:

  1. MEDIA_ROOT = os.path.join(BASE_DIR, "media")
  2. MEDIA_URL = "/media/"

Note

Please make sure both the static and media subfolders exist in your project and are writable.

Now add a little magic to the TEMPLATE_DIRS section of the file:

  1. TEMPLATE_DIRS = (
  2. # The docs say it should be absolute path: BASE_DIR is precisely one.
  3. # Life is wonderful!
  4. os.path.join(BASE_DIR, "templates"),
  5. )

Add at least one template to CMS_TEMPLATES; for example:

  1. CMS_TEMPLATES = (
  2. ('template_1.html', 'Template One'),
  3. ('template_2.html', 'Template Two'),
  4. )

We will create the actual template files at a later step, don’t worry about it for now. Simply paste this code into your settings file.

Note

The templates you define in CMS_TEMPLATES have to exist at runtime and contain at least one {% placeholder <name> %} template tag to be useful for django CMS.

The django CMS allows you to edit all languages for which Django has built in translations. Since these are numerous, we’ll limit it to English for now:

  1. LANGUAGES = [
  2. ('en', 'English'),
  3. ]

Finally, set up the DATABASES part of the file to reflect your database deployment. If you just want to try out things locally, sqlite3 is the easiest database to set up, however it should not be used in production. If you still wish to use it for now, this is what your DATABASES setting should look like:

  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.sqlite3',
  4. 'NAME': os.path.join(BASE_DIR, 'database.sqlite'),
  5. }
  6. }

Given django CMS’s support for Django 1.6.x, Django 1.7 (or later) users have to specify where the migrations are situated using the MIGRATION_MODULES setting:

  1. MIGRATION_MODULES = {
  2. 'cms': 'cms.migrations_django',
  3. 'menus': 'menus.migrations_django',
  4. # Add also the following modules if you're using these plugins:
  5. 'djangocms_file': 'djangocms_file.migrations_django',
  6. 'djangocms_flash': 'djangocms_flash.migrations_django',
  7. 'djangocms_googlemap': 'djangocms_googlemap.migrations_django',
  8. 'djangocms_inherit': 'djangocms_inherit.migrations_django',
  9. 'djangocms_link': 'djangocms_link.migrations_django',
  10. 'djangocms_picture': 'djangocms_picture.migrations_django',
  11. 'djangocms_snippet': 'djangocms_snippet.migrations_django',
  12. 'djangocms_teaser': 'djangocms_teaser.migrations_django',
  13. 'djangocms_video': 'djangocms_video.migrations_django',
  14. 'djangocms_text_ckeditor': 'djangocms_text_ckeditor.migrations_django',
  15. }

URL configuration

You need to include the 'cms.urls' urlpatterns at the end of your urlpatterns. We suggest starting with the following ~/workspace/myproject/myproject/urls.py:

  1. from django.conf import settings
  2. from django.conf.urls import include, url
  3. from django.conf.urls.i18n import i18n_patterns
  4. from django.conf.urls.static import static
  5. from django.contrib import admin
  6. admin.autodiscover() # Not required for Django 1.7.x+
  7. urlpatterns = i18n_patterns('',
  8. url(r'^admin/', include(admin.site.urls)),
  9. url(r'^', include('cms.urls')),
  10. ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Creating templates

django CMS uses templates to define how a page should look and what parts of it are editable. Editable areas are called placeholders. These templates are standard Django templates and you may use them as described in the official documentation.

Templates you wish to use on your pages must be declared in the CMS_TEMPLATES setting:

  1. CMS_TEMPLATES = (
  2. ('template_1.html', 'Template One'),
  3. ('template_2.html', 'Template Two'),
  4. )

If you have followed this tutorial from the beginning, this code should already be in your settings file.

Now, on with the actual template files!

Fire up your favorite editor and create a file called base.html in a folder called templates in your myproject directory.

Here is a simple example for a base template called base.html:

  1. {% load cms_tags sekizai_tags %}
  2. <html>
  3. <head>
  4. <title>{% page_attribute "page_title" %}</title>
  5. {% render_block "css" %}
  6. </head>
  7. <body>
  8. {% cms_toolbar %}
  9. {% placeholder base_content %}
  10. {% block base_content %}{% endblock %}
  11. {% render_block "js" %}
  12. </body>
  13. </html>

Now, create a file called template_1.html in the same directory. This will use your base template, and add extra content to it:

  1. {% extends "base.html" %}
  2. {% load cms_tags %}
  3. {% block base_content %}
  4. {% placeholder template_1_content %}
  5. {% endblock %}

When you set template_1.html as a template on a page you will get two placeholders to put plugins in. One is template_1_content from the page template template_1.html and another is base_content from the extended base.html.

When working with a lot of placeholders, make sure to give descriptive names to your placeholders so you can identify them more easily in the admin panel.

Now, feel free to experiment and make a template_2.html file! If you don’t feel creative, just copy template_1 and name the second placeholder something like “template_2_content”.

Static files handling with sekizai

The django CMS handles media files (css stylesheets and javascript files) required by CMS plugins using django-sekizai. This requires you to define at least two sekizai namespaces in your templates: js and css. You can do so using the render_block template tag from the sekizai_tags template tag library. We highly recommended putting the {% render_block "css" %} tag as the last thing before the closing </head> HTML tag and the {% render_block "js" %} tag as the last thing before the closing </body> HTML tag.

Initial database setup

django CMS uses Django 1.7’s built-in support for database migrations to manage creating and altering database tables. django CMS still offers South-style migrations for users of Django up to 1.6 but as noted above, strictly requires South>=1.0.1 in this case.

Fresh install

If you are using Django 1.7 or later run:

  1. python manage.py migrate
  2. python manage.py createsuperuser

If you are using Django 1.6.x run:

  1. python manage.py syncdb --all
  2. python manage.py migrate --fake

The call to syncdb will prompt you to create a super user. Choose ‘yes’ and enter appropriate values.

Upgrade

If you are upgrading your installation of django CMS from a previous version run:

  1. python manage.py syncdb # Django 1.6.x only
  2. python manage.py migrate

Check you did everything right

Now, use the following command to check if you did everything correctly:

  1. python manage.py cms check

Up and running!

That should be it. Restart your development server using python manage.py runserver and point a web browser to 127.0.0.1:8000 : you should get the django CMS “Installation Successful” screen.

it-works-cms

Use the new side-frame-based administration by appending ‘?edit’ to your URL as follows: http://127.0.0.1:8000/?edit. This will reveal a login form.

login-form

Log in with the user you created during the database setup.

If this is your first django CMS project, read through the tutorial for a walkthrough of the main features of django CMS.

For more information on using django CMS for managing web content, see Using django CMS.

To deploy your django CMS project on a production webserver, please refer to the Django documentation.