Sep 11

From Wordpress to Django with love

Django's got your back even if you're coming from Wordpress

Estimated reading time: 4.0 minutes, 5.0 seconds, containing: 817 words

I have to admit I admire Wordpress, specially for the community and the market penetration that is has... when you really think about it Wordpress == most prefered blogging platform.

Being a Python dude, and not a PHP dude, I use Python tools for all of my development needs, whether I am in a need of a CMS like Plone or Django-LFC, or a webframework like Django, I tend to pick up Python solutions to the PHP counterparts.

Most of the designers and some developers that I admire and tend to read have Wordpress as their blogging platform, and from time to time they implement features that I would like to have on my django-mingus blog like an estimated reading time that I got from Brian Cray's Estimated reading time in web design article (who's got a great blog that I invite you all to read by the way).

The reality is that most of the plugins that are created for Wordpress could also be created for other blogging platforms, and today I want to show you just how similar I can recreate the effect that Brian achieved on his site by using Django and Python features instead of PHP and Wordpress.

I didn't try to optimize the code or think of my own solution, but in my example I just want to show you how similar my code is compared to Brian's:

<?php
$mycontent = $post->post_content; // wordpress users only
$word = str_word_count(strip_tags($mycontent));
$m = floor($word / 200);
$s = floor($word % 200 / (200 / 60));
$est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
?>
<p>Estimated reading time: <?php echo $est; ?></p>

Now compare that code with the following django code:

from django.template.defaultfilters  import  striptags, wordcount
from math import floor
@register.simple_tag
def estimate(obj):
    mycontent = obj.body_markup
    word = wordcount(striptags(mycontent))
    minutes = floor(word / 200)
    seconds = floor(word % 200 / (200 / 60))
    estimated_time = "%s minute%s, %s second%s" % \ 
            (minutes, ('' if minutes == 1 else 's'), seconds, \
            ('' if seconds == 1 else 's'))
    return estimated_time

As you can see, Django has got striptags and wordcount filters just like PHP has, and for the django specific code all that I am asking for is the body text out of my objects, this was specific to django_mingus but in any other django models you would just substitute the body_markup with the column that contains the text that you want to take measurements from. The other difference is how you write the ternary operator, and although it is more verbose than PHP's counterpart (which is taken from C) it's still not that bad.

Now that we have our method all that we need to do is display it in our template which is done just as easily as echoing to our file in PHP:

<p>Estimated reading time: {% estimate object %}</p>

As you can see the steps that I've taken in Django are almost identical to the ones that you would have taken in Wordpress, with the added bonus from the Django side of making you write this into a function and outside of the template logic instead of having it all written in the template, but of course the same thing could have been achieved with the PHP example.

It's not about the tool, it's about the person who is using it

There are more things that I would like to implement in this blog that other Wordpress blogs have but in the end I can achieve the same results that I would have gotten out of the Wordpress plugins with a little bit of tinkering and work, and although somethings I can just plug-in apps from other people since django projects are made up of smaller apps, there is a greater satisfaction when you are the one that is making the improvements to the site on your own without just installing a ready-made solution because:

  1. At the end of the day you should strive to get better at building things not just finding the plugins to do the work for you.
  2. The only way to learn is by practicing and sometimes having ready-made solutions makes us lazy.
  3. It makes you feel good when you know that you can take a concept from a different platform and implement it in the platform that you are working with, making you more valuable for the clients that might need a certain feature that other platforms deliver.

With this post I also took the time to clean up some of the features that weren't working as they should... but if you find something that is not working fine, or you are thinking of a feature that would make it easier for you guys to read my blog... I look forward to any suggestions!

Tags: django, wordpress