Integrating applications
All the following sections of this tutorial are concerned with integrating other applications into django CMS, which is where a vast part of its power comes from.
Integrating applications doesn’t just mean installing them alongside django CMS, so that they peacefully co-exist. It means using django CMS’s features to build them into a single coherent web project that speeds up the work of managing the site, and makes possible richer and more automated publishing.
It’s key to the way that django CMS integration works that it doesn’t require you to modify your other applications unless you want to. This is particularly important when you’re using third-party applications and don’t want to have to maintain your own forked versions of them. (The only exception to this is if you decide to build django CMS features directly into the applications themselves, for example when using placeholders in other applications.)
For this tutorial, we’re going to take a basic Django opinion poll application and integrate it into the CMS. So we’ll install that, and create a second, independent, Polls/CMS Integration application to manage the integration, leaving the first untouched.
Install the polls
application
Install the application from its GitHub repository using pip
:
pip install git+http://git@github.com/divio/django-polls.git#egg=polls
Let’s add this application to our project. Add 'polls'
to the end of INSTALLED_APPS
in your project’s settings.py (see the note on The INSTALLED_APPS setting about ordering ).
Add the following line to urlpatterns
in the project’s urls.py
:
url(r'^polls/', include('polls.urls', namespace='polls')),
Make sure this line is included before the line for the django-cms urls:
url(r'^', include('cms.urls')),
django CMS’s URL pattern needs to be last, because it “swallows up” anything that hasn’t already been matched by a previous pattern.
Now run the application’s migrations:
python manage.py migrate polls
At this point you should be able to log in to the Django admin - http://localhost:8000/admin/
- and find the Polls application.
Create a new Poll, for example:
Question: Which browser do you prefer?
Choices:
- Safari
- Firefox
- Chrome
Now if you visit http://localhost:8000/en/polls/
, you should be able to see the published poll and submit a response.
Improve the templates for Polls
You’ll have noticed that in the Polls application we only have minimal templates, and no navigation or styling.
Our django CMS pages on the other hand have access to a number of default templates in the project, all of which extend one called base.html
. So, let’s improve this by overriding the polls application’s base template.
We’ll do this in the project directory.
In mysite/templates
, add polls/base.html
, containing:
{% extends 'base.html' %}
{% block content %}
{% block polls_content %}
{% endblock %}
{% endblock %}
Refresh the /polls/
page again, which should now be properly integrated into the site.
Set up a new polls_cms_integration
application
So far, however, the Polls application has been integrated into the project, but not into django CMS itself. The two applications are completely independent. They cannot make use of each other’s data or functionality.
Let’s create the new Polls/CMS Integration application where we will bring them together.
Create the application
Create a new package at the project root called polls_cms_integration
:
python manage.py startapp polls_cms_integration
So our workspace looks like this:
env/
src/ # the django polls application is in here
polls_cms_integration/ # the newly-created application
__init__.py
admin.py
models.py
migrations.py
tests.py
views.py
mysite/
static/
project.db
requirements.txt
Add it to INSTALLED_APPS
Next is to integrate the polls_cms_integration
application into the project.
Add polls_cms_integration
to INSTALLED_APPS
in settings.py
- and now we’re ready to use it to being integrating Polls with django CMS. We’ll start by developing a Polls plugin.
Note
The project or the application?
Earlier, we added new templates to the project. We could equally well have have added templates/polls/base.html
inside polls_cms_integration
. After all, that’s where we’re going to be doing all the other integration work.
However, we’d now have an application that makes assumptions about the name of the template it should extend (see the first line of the base.html
template we created) which might not be correct for a different project.
Also, we’d have to make sure that polls_cms_integration
came before polls
in INSTALLED_APPS
, otherwise the templates in polls_cms_integration
would not in fact override the ones in polls
. Putting them in the project guarantees that they will override those in all applications.
Either way of doing it is reasonable, as long as you understand their implications.