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:
- 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. - 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. - 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.
5 Comments
But now you're running Apache with both mod_wsgi and mod_php enabled, which could really not be that great for performance. Do you notice any problems from that side?
I had no problems with performance, but this is a very small deployment. In other setups I used mod_proxy to proxy requests to a python webserver like tornado or Spawn.
From way back in 2006:
http://bit.ly/biaaeE
Hi Doug, thanks for your comment! I've updated my post to reflect your advice.
How is the performance?
6 Trackbacks
[...] The rest is here: Integrate WordPress and Django — geek scrap [...]
[...] post: Integrate WordPress and Django — geek scrap Tags: book, Django, facebook, geek-scrap, how-to, linux, microsoft, Python, science, [...]
[...] Integrate WordPress and Django — geek scrap [...]
[...] reading here: Integrate WordPress and Django — geek scrap Share and [...]
[...] Excerpt from: Integrate WordPress and Django — geek scrap [...]
[...] the rest here: Integrate WordPress and Django — geek scrap Share and [...]