Integrate WordPress and Django

Last year WordPress got an award as best Open Source CMS software and the reason is clear: it’s easy to setup, low on resources, very customizable and full of useful extensions. So unless you have very specific deployment requirements and if your blog is not part of your core technology, you may get the best of both worlds by using WordPress for blogging and use a web framework for everything else. Not reinventing the wheel is very important in post-agile world, after all.

Being a Django monkey, I’d like to share some tips on how to make WordPress and Django live together:

URL mapping

To make WordPress and Django co-exist, they should map to different parts of the url space: you can simply configure them to respond on different virtual hosts, or just map WordPress to a specific path. For instance, if your webserver is Apache and you’re serving Django through mod_wsgi, you can use a config snippet like this:

<VirtualHost *:80>
ServerName somehost.com
ServerAdmin hostmaster@somehost.com
ErrorLog /path/to/somehost.com/log/error.log
CustomLog /path/to/somehost.com/log/access.log combined
DirectoryIndex index.html
DocumentRoot /var/empty
WSGIScriptAlias / /path/to/somehost.com/parts/wsgi/wsgi
WSGIDaemonProcess somehost.com user=www group=www threads=25
WSGIProcessGroup somehost.com
Alias /blog/ /path/to/somehost.com/wordpress/
Alias /media/ /path/to/somehost.com/project/media/

<Directory /path/to/somehost.com/wordpress/>
Order allow,deny
Allow from all
</Directory>

<Directory /path/to/somehost.com/media/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Accessing data

To share data between your WordPress blog and your Django instance, you have two three main options:

  1. Save your WordPress tables and your Django models in the same database and configure your Django models accordingly.
    Pros: this approach is very straightforward and you don’t need to learn WordPress API. You can use Django admin interface to edit your WordPress database.
    Cons: sharing a database between two applications can have an impact on maintenance issues. For example, to rehash your Django application, you can’t just drop database, re-create it and populate with syncdb. WordPress database schema can change in new releases and make your ORM mapping obsolete.
  2. Use Python’s xmlrpclib in Django to access WordPress XML-RPC interface.
    Pros: API changes won’t possibly break compatibility with previous releases. XML-RPC adds more logic, which means more consistency checks and more behind-the-curtain processing.
    Cons: XML-RPC calls are not as performant as direct access to MySQL.
  3. UPDATE: Doug pointed me to another method: adding a PHP template loader to Django. An useful video tutorial is also available.

To setup method 1, you can use models.py from django-wordpress-admin project, which was built around inspectdb output with some custom manager enhancements. On GitHub there’s another project named django-wordpress, with the same approach and a step-by-step tutorial is published at uswaretech.com.

If you found Django-Wordpress integration tips useful, please drop a note in the comment box below to say how you used it, or just stay in touch with Twitter or RSS Feed for more Django stories.

This entry was posted in Coding, Tips and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments