Skip to main content

Extensible Template Plugins for Django

Project description


.. image::

.. image::

.. image::

Extensible Template Plugins for Django


The full documentation is at


Install django-n3-templateplugins::

pip install dj-n3-templateplugins

Add it to your `INSTALLED_APPS`:

.. code-block:: python



If you want to build an extensible platform, you may find the need to allow the developers that use your app to alter
or add on to the functionality that you provide. This django package attempts to add 'plugin' functionality to
your django app.

To give an example of why I'm creating this... My company built an in-house django asset management system that we want to open-source, but there is a bunch of business-logic throughout the Templates and Views that wouldn't make sense in an open-sourced app. So in order to open source our asset management system we needed to make our custom business-logic pluggable. Plus, anyone that used our asset management system would probably want a similar feature for their own business logic.

How it works:

End-Users of your app create a 'plugin', which is just a Python Class which requires two methods to be created:

.. code-block:: python

class Plugin(TemplatePlugin):
def get_context_data():
# How the plugin adds extra data to your Views/Templates
return {'foo': 'some context'}

def render_html():
# How the plugin uses the context (and any provided by your view) to return HTML code (Full Django Template
# syntax is allowed here!)
return '<p><b>I am HTML</b> and I can use my context like this: {{ }}</p>'

These plugins get placed in the configurable directory:

.. code-block:: python

N3PLUGINS_PLUGIN_DIR = '/some/path/with/plugins'

You then use the provided decorator @load_plugins to decorate your views like this:

.. code-block:: python

class TestView(ListView):
model = SomeModel
template_name = 'some_template.html'

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['plugins'] = self.plugins
return context

This decorator will set a class property called self.plugins which is a dictionary. You then use this at any point
in your view's code and make sure it gets added to the Context object that is passed in for Template rendering.

In your template you then use django-n3-templateplugins provided templatetag to render the plugin's html, which looks
something like this:

.. code-block:: python

# some_template.html
{% load render_plugin %}
{% for k, v in plugins.items %}
{% render_plugin v.plugin_object %}
{% endfor %}

And that's it!

* Note: This early implementation only works with class-based views (as we set a class property with a decorator. This
isn't possible with a function).

Running Tests

Does the code actually work?


source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox


Tools used in rendering this package:

* Cookiecutter_
* `cookiecutter-djangopackage`_

.. _Cookiecutter:
.. _`cookiecutter-djangopackage`:


0.1.0 (2017-09-16)

* First release on PyPI.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj-n3-templateplugins-0.3.1.tar.gz (8.9 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page