Skip to content

Simple django wordpress integration with Django WordPress API library

image-post

Nowadays, WordPress is the most known CMS in the web. For creating and managing static content with no complex logic behind, it is a good point to start. The problem is when you need to start managing users, custom data and relate them through some degree of complexity; WordPress is not the tool to do it in the best way.

There is when Django appears, it is an awesome framework to create and personalize your website according to your needs without almost any limitations, using the thousands of Django libraries available in the web. The problem is that Django is not designed to be a CMS, so if you need it as one, it could be harder to implement than to just use WordPress. There are some Django libraries that allow to use WordPress from Django, but they are hard to intregrate and the resulting applications are hard to maintain. The other problem is that a lot of websites are already implemented in WordPress so it could be traumatic to migrate it to a Django application.

In order to make things easier, I am going to introduce you to Django WordPress Api library, DWA to his friends. It takes advantage of WP REST API v1 to connect in an easy way a WordPress blog with your Django application; allowing you to customize your Django app without giving up the tools that WordPress as a CMS presents to you. In fact, Swapps’ blog page has been created using Django, but this post has been created in WordPress and is being displayed here using this library.

How to use DWA

Let’s start with the configuration. The first thing is that you need to install WP REST API v1 plugin inside your WordPress site and enable it.
Then you need to install Django WordPress Api library inside your django application

pip install django-wordpress-api

After that, add wordpress_api inside the installed apps

INSTALLED_APPS += ('wordpress_api',)

Also, if you are using DWA Views (explained later) you need to add the wordpress_api urls in your application urls

url(r'^blog/', include('wordpress_api.urls')),

Finally, you need to set up the DWA required settings; WP_URL and BLOG_POSTS_PER_PAGE.

WP_URL = http://your-wordpress-app.com/
BLOG_POSTS_PER_PAGE = number-of-blogs-to-display-per-page

and that’s it.

DWA has two ways to be used, using its views or using its client. The views allow to retrieve and display the blog, retrieve posts details, retrieve posts by tags or by categories and search by keywords; the same as in a simple WordPress blog. This views alone will allow you to display a basic blog, as the one you are reading right now. You can see more information about it inside the documentation.

On the other hand, if you need something more complex you could use the wordpress_api client. It allows you to retrieve other kinds of post, beyond the “post” type, order them by another field more than “date” and use all the filters that WP API v1 allows. This could give you more control and customization inside your Django blog if your app requires it. This process is very simple, the wordpress_api client bases itself in just one main method: get_posts. You just need to provide this method with your requirements and you are ready to go. For example, the following method call will return all the ‘analysis’ type posts written by me that are in the second page, ordered by title; considering each page with the number of blogs per page defined in the settings.

wp_posts = get_posts(wp_filter={'author': 'jariza'},
                     page_number=2, orderby='title',
                     custom_type='analysis')

In conclusion, Django WordPress Api allows you to integrate your WordPress blog with your Django application using the most transparent way: an API. This way, you will be able to take advantage of all the CMS potential that WordPress provides and all the customization, personalization and simplicity that makes Django one of the best web framework out there.

The one little disadvantage is that you need to have two applications living: the WordPress one and the Django one. For already existing WordPress applications, there is no problem at all; you may simply integrate it with your django site. On the other side, if you are starting from scratch it may be harder to have two living sites; but it is easier to implement and to manage. Keep in mind that your CMS will be isolated from your actual site so you will not have to worry about bugs or misbehaviors produced by code dependencies.

If you need more information, you may always read the Django WordPress Api documentation directly.