A short example using django-app-plugins
Posted on | November 27, 2008 | 7 Comments
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:
1 | {% plugin_point "shop_sidebar" %} |
And in our shop/__init__.py file, we set up the plugin point like so:
1 2 3 4 5 | 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
1 2 3 4 5 6 7 8 | {% load i18n %}
{% if user.is_authenticated %}
<p class="wishlist">
<a href="{% url satchmo_wishlist_view %}">
{% trans 'Wishlist' %}
</a>
</p>
{% 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
Technorati Tags: django-app-plugins, django
Comments
7 Responses to “A short example using django-app-plugins”
Leave a Reply
November 28th, 2008 @ 4:36 pm
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
November 28th, 2008 @ 6:04 pm
Thanks Matthew, I’ve fixed the code sample.
?December 2nd, 2008 @ 12:30 pm
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.
December 30th, 2008 @ 9:04 pm
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
June 13th, 2009 @ 6:15 am
Hi,
Tried to download the test. No luck. Is it still available somewhere?
Cheers
June 16th, 2009 @ 10:41 am
Fixed, I’d somehow deleted the files directory during upgrade to the latest Wordpress.
?June 16th, 2009 @ 11:22 am
Cheers for that