Apphooks

Right now, our Django Polls application is statically hooked into the project’s urls.py. This is all right, but we can do more, by attaching applications to django CMS pages.

Create an apphook

We do this with an apphook, created using a CMSApp sub-class, which tells the CMS how to include that application.

Apphooks live in a file called cms_apps.py, so create one in your Polls/CMS Integration application, i.e. in polls_cms_integration.

This is a very basic example of an apphook for a django CMS application:

  1. from cms.app_base import CMSApp
  2. from cms.apphook_pool import apphook_pool
  3. from django.utils.translation import ugettext_lazy as _
  4. @apphook_pool.register # register the application
  5. class PollsApphook(CMSApp):
  6. app_name = "polls"
  7. name = _("Polls Application")
  8. def get_urls(self, page=None, language=None, **kwargs):
  9. return ["polls.urls"]

Instead of defining the URL patterns in another file polls/urls.py, it also is possible to return them directly, for instance as:

  1. from django.conf.urls import url
  2. from polls.views import PollListView, PollDetailView
  3. class PollsApphook(CMSApp):
  4. # ...
  5. def get_urls(self, page=None, language=None, **kwargs):
  6. return [
  7. url(r'^$', PollListView.as_view()),
  8. url(r'^(?P<slug>[\w-]+)/?$', PollDetailView.as_view()),
  9. ]

What this all means

In the PollsApphook class, we have done several key things:

  • The app_name attribute gives the system a way to refer to the apphook - see Attaching an application multiple times for details on why this matters.
  • name is a human-readable name for the admin user.
  • The get_urls() method is what actually hooks the application in, returning a list of URL configurations that will be made active wherever the apphook is used.

Restart the runserver. This is necessary because we have created a new file containing Python code that won’t be loaded until the server restarts. You only have to do this the first time the new file has been created.

Apply the apphook to a page

Now we need to create a new page, and attach the Polls application to it through this apphook.

Create and save a new page, then publish it.

Note

Your apphook won’t work until the page has been published.

In its Advanced settings, choose “Polls Application” from the Application menu, and save once more.

select the 'Polls' application

Refresh the page, and you’ll find that the Polls application is now available directly from the new django CMS page.

You can now remove the mention of the Polls application (url(r'^polls/', include('polls.urls', namespace='polls'))) from your project’s urls.py - it’s no longer even required there.

Later, we’ll install a django-CMS-compatible third-party application.

Important

Don’t add child pages to a page with an apphook.

The apphook “swallows” all URLs below that of the page, handing them over to the attached application. If you have any child pages of the apphooked page, django CMS will not be able to serve them reliably.