Building reusable Django apps

By : Shabda Raaj

Recently I updated a lot of our old apps to be compatible with Django 1.0 and be reusable. Before we go further, let me define a _ Reusable Django App _.

A Django app is reusable if

*  It is app. (That is, it is not a project, and has no
* It can be dropped in an existing project, and only places where the project should need editing are `` `installed_apps` and the projects level ``.
* It can be used in the project with an arbitrary url prefix.

The updated apps were written initially for Django 0.96 and were coupled to the projects, so it was not possible to include them in other projects. After updating these I was able to include them in in a single project as a part of this site.

The apps were,

The following apps from

Without further ado hints to help you make your apps reusable,

Always use models.permalink with get_absolute_url type of functions:

If you have code like

def get_absolute_url(self):
    return '/project/%s/' %

this is tying your app to a specific url structure. (and hence to a specific project). Instead a code like,

def get_absolute_url(self):
    return ('app.views.view_func', [])

allows you to use this app with any arbitrary url structure in the project.

Always use reverse when you use HttpResponseRedirect.

For a similar reason as 1. Of course this does not apply if you are doing return HttpResponseRedirect('.')

Always use {% url %} with <a href=".

For reasons as in 1.

Do all imports assuming your apps exist on Pythonpath. So your imports should look like
from myapp.models import ModelClass

and not

from myproject.myapp.models import ModelClass

as this ties the app to a specific project.

Use named urlpatterns. Prefix app name to url patterns names.

Named url patterns make point 2 and 3 much simpler. Prefixing appname to url pattern name makes removes name collision and simplifies using reverse and {% url %}.

Serve your static media from /MEDIA_URL//:

Each app could be using a stylesheet named style.css. If we were serving media from user /MEDIA_URL/, we are tied to one stylesheet for the whole project. If you were serving it from /MEDIA_URL// it is as simple as symlinking a directory.

After using these pointers, your apps are decoupled from the project. To use the apps with Usware site, all we needed to do was to add the following lines to svn:externals

pastebin   site_media/kamp

And put the apps in settings.INSTALLED_APPS and include the app in the projects

Need an Amazing Webapp built? Talk to Usware. Need help with any of these apps? Talk to Usware.

Related Posts

Can we help you build amazing apps? Contact us today.

Topics : tips
© Agiliq, 2009-2012