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
[tags]django-app-plugins,django[/tags]
Related posts:
- A Django Killer App – Web Table Data Yet another great use for Django is to make trivial...
- Zyons Forum for Django This summer, I've been converting my biggest site, Invisible Castle,...
- Django and multipart emails As part of the Django Storefront system I am writing,...
- Speedy Django Development As I’ve mentioned before, I am building my own web...
- Helpful Django utilities and links Two simple links which have been very helpful in doing...
Useful post! Thanks for pointing me to django-app-plugins.
And perhaps you should double check that last code sample. It looks like it was eaten by something. (Is your blog run by under-paid velociraptors, by chance?)
MWM
Thanks Matthew, I’ve fixed the code sample.
I have been reverse-engineering my way through pinax/django until i came across app-plugins (used in the profile page) .Been trying to figure how in the hell it works but there was absolutely no docs or anybody that seemed to know what i was talking about. Very happy i came across your site.
Hey Bruce!
How ARE you?? I assume you’re still doing web stuff. I dropped CC TM – too much else going on, but still doing Progressive Voices. Also my blog for writing practice.
I’m about to connect you up with (or vice versa) my son, Wylie, who’s trying to complete a website for a movie producer and is having trouble mounting it (he’s not a programmer…)
Would be fun to have lunch one of these weeks…
Joy
Hi,
Tried to download the test. No luck. Is it still available somewhere?
Cheers
Fixed, I’d somehow deleted the files directory during upgrade to the latest WordPress.
Cheers for that