Photo by Matthieu Comoy on Unsplash

4 (+1) Django CMS tips

Luca Fedrizzi
Life in Belka
Published in
3 min readMar 16, 2018

--

I managed to group together several tips about Django CMS working with it for the last month and talking with expert developers.

Some of these tips may seem obvious, but I think that when there is too much magic you tend to see just that.

Imagine that you are sitting at a bar, with a drink in hand and you are talking to a developer friend of yours.

At some point you start talking about how beautiful Django is and how much Django CMS is convenient to develop sites. If I was that friend I would suggest you:

1. Read the source code

You are trying to solve a problem that you thought was trivial and common to many other developers.

After a deep research into the documentation and googling like crazy you have not found anything really useful yet.

What ya gonna do?

Go immediately on the Github repository and read the source code of the official plugin with the closest features to those you want to develop.

The real documentation is in the comments!

Since the code is well commented I suggest it as the first place to look at to find in-depth documentation.

2. Creation of templates

I spend most of the time adding components to pages, but the default templates are not enough if you need to build complex layouts and easily editable contents.

Since it wasn’t always immediate I’ve learned how they work on the code that I've found on official repositories! (Thank you advice number one 😎)

At one point I came across a piece of code like this:

def render(self, context, instance):
context['instance'] = instance
return context

This is a method of a plugin class and it says more than what it seems.

This piece of code clarify how the plugin's template receives the informations managed by the related model.

  • context: you render the template with a Context.
  • instance: contains all the data from the model that you want display.

So, before returning the context, you can modify all the data in an instance.
E.g. you can let the user change the template:

# ...if instance and instance.template: 
self.render_template = instance.template
context['instance'] = instance
# ...

3. Selection of themes for plugins

A plugin can have more than one template. You can understand it by looking at the code of this plugin:

It's very useful to know this, because it allows you to extend existing plugins that do not show the contents as you would like to. So why creating a new component when you can extend an existing one?

4. Revert to live

After some edits on a page I realised that I deleted some good content. Recreating it would have taken a lot of time; and why should I rewrite things anyway?

How do I revert it? 🧐

In the edit toolbar at the top of the page there is the entry “Page”, in its submenu there is the entry “Revert to live”. This magic function deletes all the changes made that have not been published.

I think that's hard to find this function unless you know where it is!

5. Bonus — djangocms-history

tl;dr cmd+z

djangocms-history allows you to go back and forth between changes on a page. It doesn't delete all edits like “Revert to live”, so it’s ideal for small corrections. If installed correctly, it adds two convenient buttons in the page edit toolbar.

Final thoughts

Django CMS is a library that rewards those who can observe.

The lack of examples in the documentation requires a little more time to learn how to work with it.

Did you find yourself having any difficulties while developing on django-cms? Let me know in the comments ✒️

--

--

Luca Fedrizzi
Life in Belka

I’m Luca, I help build products and grow businesses. I try to bring clearness by providing new perspectives. • 🍜