Example plugin for DJ Press
Project description
Example Plugin for DJ Press
DJ Press is a blogging application for Django sites inspired by classic WordPress.
As of version 0.12, plugin support has been added, and this repository serves as an example of a simple plugin that uses the following DJ Press hook to add some text before your blog posts:
pre_render_content
- this is called before the plain Markdown text has been converted to HTML.
Plugin Development Steps
These are the specific steps that were followed to create this plugin. These steps were performed on a "Unix-like"
system but should work fine on Windows using WSL, macOS, or Linux. I use the uv
tool from Astral here, but if you
have another preferred tool, you should be able to follow along and modify for your own use-case. You will also need
a test Django site locally with DJ Press already installed and configured. Creating this Django site is out of scope
for these instructions; the assumption is that if you are wanting to develop a DJ Press plugin, you will have already
have access to a local Django site with DJ Press for testing.
-
Create a parent directory to store the plugin:
mkdir djpress-example-plugin
-
Initialise the project with a
pyproject.toml
file and recommended files and directories:uv init --package
-
Remove the contents of
__init__.py
and update thepyproject.toml
file with the following changes:- Remove the
project.scripts
section - Change the
requires-python
section to:requires-python = ">=3.10"
. This is because DJ Press supports Python 3.10 or newer, and so it is recommended that your plugin package does the same. If you are not planning to distribute your package to the public, then you can set this to your preferred version of Python. - Edit the rest of your file to suit your needs, e.g.
version
,description
,authors
, etc.
- Remove the
-
Add
djpress
as a dependency of your project:uv add djpress
-
Add any other dependencies that you require.
-
Create the plugin module:
touch src/djpress_example_plugin/plugin.py
, and open in your favourite editor. This module does not need to be calledplugin.py
, but it makes the configuration and discovery of the plugin easier, so is highly recommended. The following steps are all done insrc/djpress_example_plugin/plugin.py
. -
Import
DJPressPlugin
which is the base class that this plugin will inherit from:from djpress.plugins import DJPressPlugin
-
Create a new class called
Plugin
that inherits fromDJPressPlugin
- again, this does not need to be calledPlugin
, but makes configuration and discovery easier, so is highly recommended:class Plugin(DJPressPlugin):
-
Give the plugin a name:
name = 'djpress_example_plugin'
. The name is used by DJ Press to discover related settings and while this could be anything you like, it is highly recommended to make this the same as the package name. -
Create a
setup
method that takesregistry
as an argument:def setup(self, registry):
. The registry stores all the plugins that have registered with hooks. This is loaded at application start up. -
In the
setup
method, you register your plugin functionality with the appropriate plugin hook using theregister_hook
method from theregistry
, e.g.registry.register_hook("pre_render_content", self.add_greeting)
-
Now create a method containing your plugin functionality:
def add_greeting(self, content):
. This method is passedcontent
which is the plain Markdown text before it has been converted to HTML. -
For this example, we will also include some configurable text that can be stored in the
PLUGIN_SETTINGS
dictionary. Plugin-specific configuration is loaded as a dictionary calledconfig
. All that our silly example does, is add a greeting before the blog post content:return f'Hello, {self.config.get("greeting_text")}!\n\n{content}'
-
That is the end of the plugin development. The next step is to install the plugin in a Django site to test that it works. To do this, you will need access to a test Django site locally. Navigate to the directory that stores your site and install your plugin package as an editable package. How to do this depends on your specific environment, but if you are using
uv
and your test site is in an adjacent directory, you could do something like this:uv add --editable ../djpress-example-plugin
. The trick will be to get the path to your plugin correct. -
Once the package is installed, open your Django settings file in your favourite editor and add the following settings to the
DJPRESS_SETTINGS
configuration object:# DJPress settings DJPRESS_SETTINGS = { # ... existing settings ... "PLUGINS": [ "djpress_example_plugin", ], "PLUGIN_SETTINGS": { "djpress_example_plugin": { "greeting_text": "world", }, }, }
-
If everything has worked, you should be able to run your Django site, open a blog post, and you'll see:
"Hello, world!"
before the blog post content. And if you view the source, you'll see that the text is wrapped in paragraph tags:<p>Hello, world!</p>
. This is because the hook we used ran before the content was converted to HTML.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file djpress_example_plugin-0.1.0.tar.gz
.
File metadata
- Download URL: djpress_example_plugin-0.1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa7507ca431c73e6e3bf48a76bc3f21cedf38bba1e68c3fd0bfd1555ac4628a8 |
|
MD5 | 93b175de8f03c5e796d2912db5cc7b31 |
|
BLAKE2b-256 | eb8ab39cf0b779b6d2b9a24aaca916b89bfc8a3c1017d3401fd4fcbb115c4ce5 |
File details
Details for the file djpress_example_plugin-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: djpress_example_plugin-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 992ea824798c3e5243aff126ed300d8d9af6063ca5479aafcba36d932c1c5ddc |
|
MD5 | b0d398829356a2e66e04e9529ce4ad93 |
|
BLAKE2b-256 | 10b0133e25704d65e154dc308f002636ca771defe69701947f0e3d128cbcad88 |