It is pretty easy to integrate your own Django applications with django CMS. You have 5 ways of integrating your app:
Menus
Statically extend the menu entries
Attach Menus
Attach your menu to a page.
App-Hooks
Attach whole apps with optional menu to a page.
Navigation Modifiers
Modify the whole menu tree
Custom Plugins
Display your models / content in cms pages
With App-Hooks you can attach whole Django applications to pages. For example you have a news app and you want it attached to your news page.
To create an apphook create a cms_app.py in your application. And in it write the following:
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _
class MyApphook(CMSApp):
name = _("My Apphook")
urls = ["myapp.urls"]
apphook_pool.register(MyApphook)
Replace myapp.urls with the path to your applications urls.py.
Now edit a page and open the advanced settings tab. Select your new apphook under “Application”. Save the page.
Warning
If you are on a multi-threaded server (mostly all webservers, except the dev-server): Restart the server because the URLs are cached by Django and in a multi-threaded environment we don’t know which caches are cleared yet.
Note
If at some point you want to remove this apphook after deleting the cms_app.py there is a cms management command called uninstall apphooks that removes the specified apphook(s) from all pages by name. eg. manage.py cms uninstall apphooks MyApphook. To find all names for uninstallable apphooks there is a command for this as well manage.py cms list apphooks.
If you attached the app to a page with the url /hello/world/ and the app has a urls.py that looks like this:
from django.conf.urls.defaults import *
urlpatterns = patterns('sampleapp.views',
url(r'^$', 'main_view', name='app_main'),
url(r'^sublevel/$', 'sample_view', name='app_sublevel'),
)
The main_view should now be available at /hello/world/ and the sample_view has the url /hello/world/sublevel/.
Note
All views that are attached like this must return a RequestContext instance instead of the default Context instance.
If you’d like to use application namespaces to reverse the URLs related to your app, you can assign a value to the app_name attribute of your app hook like this:
class MyNamespacedApphook(CMSApp):
name = _("My Namespaced Apphook")
urls = ["myapp.urls"]
app_name = "myapp_namespace"
apphook_pool.register(MyNamespacedApphook)
As seen for Language Namespaces, you can reverse namespaced apps similarly:
{% url myapp_namespace:app_main %}
If you want to access the same url but in a different language use the language templatetag:
{% load i18n %}
{% language "de" %}
{% url myapp_namespace:app_main %}
{% endlanguage %}
What makes namespaced app hooks really interesting is the fact that you can hook them up to more than one page and reverse their URLs by using their instance namespace. Django CMS takes the value of the reverse_id field assigned to a page and uses it as instance namespace for the app hook.
To reverse the URLs you now have two different ways: explicitly by defining the instance namespace, or implicitely by specifiyng the application namespace and letting the url templatetag resolving the correct application instance by looking at the currently set current_app value.
Note
The official Django documentation has more details about application and instance namespaces, the current_app scope and the reversing of such URLs. You can look it up at https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces
When using the reverse function, the current_app has to be explicitly passed as an argument. You can do so by looking up the current_app attribute of the request instance:
def myviews(request):
...
reversed_url = reverse('myapp_namespace:app_main',
current_app=request.current_app)
...
Or, if you are rendering a plugin, of the context instance:
class MyPlugin(CMSPluginBase):
def render(self, context, instance, placeholder):
...
reversed_url = reverse('myapp_namespace:app_main',
current_app=context.current_app)
...
If you want to display content of your apps on other pages custom plugins are a great way to accomplish that. For example, if you have a news app and you want to display the top 10 news entries on your homepage, a custom plugin is the way to go.
For a detailed explanation on how to write custom plugins please head over to the Custom Plugins section.