We love designing and developing websites, but what really drives us is solving problems and cultivating strong relationships with our clients
Building reusable Django apps
By : shabda
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 settings.py/manage.py.)
* It can be dropped in an existing project, and only places where the project should need editing are `settings.py` `installed_apps` and the projects level `urls.py`.
* 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 7days7apps.com
- pastebin demo source
- jobs demo source
- pollngo demo source
- todidlist demo source
- mytym demo source
- answrs demo source
- kamp demo source
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/' % self.name
this is tying your app to a specific url structure. (and hence to a specific project). Instead a code like,
@models.permalink
def get_absolute_url(self):
return ('app.views.view_func', [self.name])
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/
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
blogango https://svn.uswaretech.com/blogango/trunk/blogango/
djikiki https://svn.uswaretech.com/djikiki/trunk/djikiki/
...............
pastebin https://svn.uswaretech.com/7days7apps/trunk/djpaste/pastebin site_media/kamp https://svn.uswaretech.com/7days7apps/trunk/kasekamp/site_media/kamp/
And put the apps in settings.INSTALLED_APPS and include the app urls.py in the projects urls.py.
Need an Amazing Webapp built? Talk to Usware. Need help with any of these apps? Talk to Usware.
Comments
- How to use pep8.py to write better Django code
- Screencast: Django Tutorial Part 1
- How and why to use pyflakes to write better Python
- Getting started with South for Django DB migrations
- A brief overview of Vagrant
- Writing jQuery plugins using Coffeescript
- Behind the Scenes: Request to Response
- Using SQLite Database with Android
- Haml for Django developers
- Coffeescript for Python programmers
- rails
- django
- linkroundup
- django opinion
- opinion
- business
- API
- appengine
- python
- satire
- startup
- Uncategorized
- marketing
- personal
- rambling
- search
- interviews
- seo-interviews
- 5startupideas
- ideas
- seo
- tips
- forms
- paypal
- utilities
- datetime
- web2.0
- Amazon
- algorithms
- presentations
- products
- pinax
- satchmo
- ecommerce
- microsoft
- yahoo
- book
- tutorial
- models
- aggreagtion
- meta
- India
- apps
- about
- CSS
- Design
- wordpress
- test slug
- vim
- urls
- reviews
- javascript
- xmpp
- emacs
- Typography
- Grid Theory
- Color Theory
- iphone
- android
- titanium
- mobile applications
- CSS3
- Browser Compatibility
- mobile
- jobs
- lamson
- django setup
- files
- upload
- jsTree
- hierarchical view
- web page
- Treeview
- coffeescript
- request
- response
- South
- django south
- django migration
- --fake
- screencasts
- February 2012
- January 2012
- December 2011
- October 2011
- September 2011
- July 2011
- June 2011
- April 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- June 2010
- April 2010
- March 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- April 2009
- March 2009
- February 2009
- November 2008
- October 2008
- June 2008
- May 2008
- April 2008
Just wanted to pop in and say that this post has been very helpful to me. I am still a Django novice, but I am wanting to drop one of my apps out, and reuse it in other Django projects of mine. Thank you for pointing me in the right direction.