A short example using django-app-plugins

At Djangocon a couple months ago, I was intrigued to hear about Django App Plugins. This small app adds the ability to make mount points in your Django templates.

That way, you can have your apps insert text into your templates just by referencing the mountpoint in the template. This allows you to have apps which possibly aren’t central to your main application add text which you wouldn’t want to put into the default template.

Unfortunately, there is absolutely no documentation, so I spent the evening working out how to get this to work. There are tons more options than what I’ve figured out, I think a simple example is much better than no documentation at all.

Let me give a real-world example. In Satchmo, we have our core apps which are always loaded. It is perfectly fine to assume those will be there when building a template. But what about the optional apps such as “wishlist?”

Those can’t get added to the main templates without forcing everyone to add the wishlist app to their projects even if they don’t plan to use that functionality.

Using django-app-plugins, this becomes very easy to set up. In our base template, we just have to put this tag:

{% plugin_point "shop_sidebar" %}

And in our shop/__init__.py file, we set up the plugin point like so:

def shop_sidebar(point, context, user, *args, **kwargs):
'''A section on the sidebar of the base screen'''
return { }

register.plugin_point(takes_context=True, takes_user=True)(shop_sidebar)

Lastly, we add a new template at templates/wishlist/plugins/shop_sidebar.html

{% load i18n %}
{% if user.is_authenticated %}

{% trans ‘Wishlist’ %} {% endif %}

It works! If “wishlist” is enabled as an app, then we get the wishlist entry on the sidebar, if not, then it is skipped without any unnecessary cross-app knowledge or entangling.

If you’d like to see the test django project I used to work out the basic usage, I’ve zipped it up for you here: app-plugin-test.zip

Coders Eye - Web Dev Tutorials and How-To Guides for Beginners
Enable registration in settings - general