Stumpy Version 1.5 is Now Available!

Stumpy Version 1.5 is now out and ready for testing.
Should be functional, but please submit any issues you might find to the issues page.

Changes include (from commits):

  • Removed hardcoded domains from JS. We now pipe in through the stumpy_domain var by setting a global JS var ‘DOMAIN’ in the index and then reference that JS var in our scripts. You will no longer need to set your domain as hard coded into javascript code.
  • Updated to use get submission of new URLs for shortening. This should work, but needs to be tested on the production version to make sure WSGI and such are working okay without the earlier hack that was implemented.

Accessing Django Template Variables in Javascript

How do we follow the Django DRY principle and avoid as many hard coded elements as possible in our project when we need to include Javascript? A good example of this is if we are pulling in a bookmarklet that will be offered on the site. We need to reference the domain of our project in the bookmarklet, but this then yields code buried in our project’s static files that needs to be altered by anyone that might be utilizing your open sourced code.

This example presents a little bit of a challenge. If this were HTML in our template we could simply reference a DOMAIN variable that we would feed in through a view. However, this is a static Javascript file, so how do we go about utilizing Django template variables in supposedly static Javascript?

Here is a simple method. In our HTML template, we will setup a Javascript global variable that is created to reference a template variable that we feed in from our view, here a domain, and this variable is then referenced in our static Javascript files.

<!-- Our HTML Template (templates/index.html) that is fed data from -->
{% extends "base.html" %}
{% block content %}

<script type="text/javascript" src="/static/somejs.js"></script>

var DOMAIN = "{{ site_domain }}";

<!-- rest of our template, business as usual. -->
{% endblock %}

As you can see, we would feed in a site_domain variable from that might be created something like this.

from django.contrib.sites.models import Site
from django.shortcuts import render_to_response

def index(request):
    '''Our main index view.'''
    site_domain = smart_str(Site.objects.get_current().domain)
    # more of our view code...
    return render_to_response('templates/index.html', {
	'site_domain': site_domain, of our template vars

Now, how do we use this in our static Javascript? We reference that global Javascript variable we declared in our template using our Django template variable site_domain.

// Our Javascript file ... /static/somejs.js 
alert("Your current domain is: "+DOMAIN);

Now our code is much more portable, even in places where things should essentially be static.

Cleansing HTML in Django Forms

We want to accept some HTML, while stripping out the rest, in our Django form. Let’s take a quick look at how we can introduce some basic HTML cleaning functionality to our Django ModelForm. We will be using Bleach to do all the dirty work.

from yourapp.models import YourModel
from django import forms
import bleach

class YourForm(ModelForm):
    class Meta:
        model = YourModel
    def bleachData(self, data, whitelist=[]):
        allowed = whitelist
        clean_data = bleach.clean(data, allowed)

        return clean_data

    def clean_somefield(self):
        somefield = self.cleaned_data['somefield']
        whitelist = ['b', 'i']
        somefield = self.beachData(somefield, whitelist)

        return somefield 

    def clean(self):
        cleaned_data = super(YourForm, self).clean()
        self.cleaned_data['somefield'] = self.clean_somefield()

        return self.cleaned_data

As you can see, we run our normal form validation methods and then initiate a post-cleanse cleanse by bleaching ‘somefield’ and allowing a whitelist of tags, bold and italics.

Stumpy is Now Available!

Stumpy has gone v1.0 and is now available on Github. The project is a URL shortener that I originally wrote in Python and have recently rebuilt from scratch to use the Django Python web framework. Now that I’ve reached what I felt was 1.0 functional code, I’ve moved the repository to the Github public section and is available under a BSD license.

Stumpy goes open source on Github

In brief, I have sucked in the first parts of the README.textile file you can find in the repository which should do most of the explaining as to the what and why. The full file contains instructions on setting everything up and how to get testing it out. If you should have any questions, please feel free to contact me. However, I’ve tried to keep the README as clear as possible to be able to drop into a Django environment and the code should be pretty self explanatory.


Mutaku URL Shortener


STUMPy is a URL shortener written by xiao_haozi (Mutaku) using the django python web framework.


There are many url shorteners out there, and STUMPy does not do anything groundbreaking.
However, there are several benefits that encouraged it’s development:

  • you keep all the data and can access it at your will
  • you keep all the code and can access/change it at your will
  • simple to use, simple to run, and simple code
  • because of it’s simplicity, it is easy to understand how url shorteners work and some of the possible optimizations
  • uses the django framework which allows for easy expansion, management, and tweaking
  • django also allows for a nice web UI for administration of all of the data


Everything is still in active development, so feel free to watch the repository to keep up with it’s evolution on Github. I will soon have the active Mutaku version of Stumpy up and running which I will post as soon as it’s up so that you can see the code in action.

Head over to the home at Github and clone or fork the source and have at it. Please be sure to add any comments or problems to the issues page on Github and feel free to send in pull requests if you’ve added any fun, interesting, or helpful code.