Tutorial Django Dojo DataGrid

April 18, 2011 No comments yet

Dojo is an open source Javascript toolkit. It provides an easy way to access and modify the DOM as well as a rich user interface widget library. One of the most powerful widgets in the Dojo toolkit is DataGrid. It provides an easy way to represent what is essentially a cross between a spreadsheet and a table. In a recent Django project I needed to output the contents of a model into the DataGrid. Thanks to django-dojoserializer this is very easy.

Setup

We need the Python package django-dojoserializer. This can be installed by downloading the source and running

python setup.py install

or with setuptools using

easy_install django-dojoserializer

or with pip using

pip install django-dojoserializer

Then add django-dojoserializer to the INSTALLED_APPS tuple in settings.py.

Dojo will need to be linked correctly in the Django template. We need to require the dojox.grid.DataGrid and dojo.data.ItemFileReadStore classes.

<script src="/static/libs/dojo/dojo.js" djConfig="parseOnLoad: true">%lt;/script>
<script type="text/javascript">
dojo.require('dojox.grid.DataGrid');
dojo.require('dojo.data.ItemFileReadStore');
</script>

Django Model

PROXY_TYPE_CHOICES = (

	('SOCKS4', 'SOCKS4'),
	('SOCKS5', 'SOCKS5'),
	('HTTP', 'HTTP'),

)

class Proxy(models.Model):
	ip = models.CharField(max_length=50)
	type = models.CharField(max_length=6, choices=PROXY_TYPE_CHOICES)
	alive = models.BooleanField(default=False)
	added = models.DateTimeField(auto_now=False, auto_now_add=True)
	last_checked = models.DateTimeField(auto_now=True, auto_now_add=False)

	def __unicode__(self):
		return self.ip

Django View

The view needs…

Using Celery with Django

April 4, 2011 No comments yet

Introduction

In a recent Django project I needed to issue some HTTP requests to a website, save the results and then display the information to the user. I could have issued those HTTP requests in a Django view, however the requests could take up to 10 seconds to return, and I didn’t want the page hanging that long for the user. I ended up using Celery, an asynchronous task queue written in Python, to execute the requests.

Installation

Celery
Celery is in pypi so installation is simple.

easy_install celery
pip install celery

Or download the source code, extract it and

python setup.py install

Django + Celery
In a high traffic production environment Celery should be paired with the RabbitMQ backend. However for my low traffic application I went with the django-kombu backend. django-kombu uses the Django database as a message store and requires very little configuration compared to other backends. We also need the djcelery package.

easy_install django-kombu
easy_install django-celery

Configuration

Before we can start using Celery we need configure our Django project. In settings.py make sure you add djkombu and djcelery to the INSTALLED_APPS tuple. Also make sure your database is configured and working.

Also add…

Tutorial: Django Template Tags

January 10, 2011 No comments yet

Introduction

In the Python Web Framework Django, template tags can be used to modify data that is about to be output in a Django template file. In this tutorial we will learn how to create and use our own template tags in a Django application.

Setting Up

Template tag definitions are stored in Django apps. The template tags in each app should be relevant to the models and views in that app. You shouldn’t have one app that stores every single template tag you use throughout your website. Each app can contain a directory called templatetags and assuming the app is added to settings.INSTALLED_APPS, Django will automatically search that appname/templatetags/ directory for template tag definitions. Remember to put an __init__.py file in each templatetags directory because the template tags inside them are imported.

Example

In an application I worked on recently I needed to display some data about a World Of Warcraft character such as their race, class and gender. The information about the character was stored in a database table and was referenced like so:

This means that if the character was a Male Orc Shaman, then the…

Django Authentication Backends

August 18, 2010 No comments yet

If you are building a Django frontend for a legacy application chances are you will need to integrate your existing user database table(s) with the the Django authentication system (django.contrib.auth). In this tutorial we will look at writing our own authentication backend and plugging it into django.contrib.auth.

Getting Started

Authentication backends are pure python classes. Given a username and password, they are expected to validate them against an authentication source (such as a database) and return a User object. There is no requirement as to where the class is placed, however I always put it in the same directory as the application that handles registrations and logging in.
When a user logs in, Django will check the AUTHENTICATION_BACKENDS (tuple) setting and iterate through all backends until one returns true. The default authentication backend is django.contrib.auth.backends.ModelBackend.

Writing Our Backend

Our backend must have two methods: authenticate and get_user

authenticate takes two arguments – username and password. It is expected to return a User instance or None. The authenticate method should check the given username and password against the user table of the legacy application. If the credentials match, then create a new User instance, save it, and return it. If…