Very simple multilingual django-cms news extension

4. April 2013. Tagged django, cms, python, work.

We recently started to internationalize our website. Although we developed a simple cms ourselves, it didn’t have as many features as it should have in my opinion, so we decided to switch over to django-cms. This was done because we could keep most of the template structure and the infrastructure we had yet.

Unfortunately there is no decent news module we could use, as all I could find are not properly working anymore. So I decided to write the simplest version of a news module I could possibly write.

So let’s start off with some theory. You will need two classes. One is a subclass of CMSPluginBase, that will tell django-cms that there is a plugin to consider. And secondly you will need a subclass of CMSPlugin, which will be translated to a table in your database and host concrete instances of your plugin. So if you add it to a placeholder of a page that instance will be saved there.

In practice that means that you will need a folder named news, with the following structure (check out my structure here):

1
2
3
4
5
6
7
news
-> __init__.py # this file is empty
-> cms_plugins.py
-> models.py
-> templates
---> news_overview.html
---> news.html

Interestingly the whole logic for rendering is in the plugin and only the settings for the instances of the plugin are in the model.

My solution is, due to it’s simplicity, also very specific and limited. To be able, to make it this simple we don’t have a specific model for the contents (in this case this could be a very specific news content type). Instead we make use of the pages feature of the cms, which already has support for multiple languages. Thus the plugin lacks some features like specific fields (publication, author and on). Instead it just considers all pages that are children of the page that contains the plugin that is being rendered right now, that are available in the current language, use the right language and are published.

The used fields are the date of publication, the title and the block named content. This can be easily changed in the template though. You can also write your own news template.

To use this plugin you can just clone the repository to your django-cms directory, add it to the INSTALLED_APPS variable and add the template news.hml to the TEMPLATES-variables. You will definitely have to adjust that template to extend your base template and define the right block. Then sync your database with syncdb --all and you can add the plugin to a page. The only setting available for now is the number of news items to display.

You can check out the source here.

In the future I might add some more features, to improve customization, but for now it fits my needs.