David Larlet is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againhg clone https://bitbucket.org/david/djangorators/wiki
Issue
Most of the time your Django views look like this:
def my_view(request, args):
# deal with args
# deal with permissions
# do some useful stuff
# deal with rendering
return an http response
I think it's a bit silly to continuously copy/paste boring parts and that's exactly the place where decorators can be powerful.
Solution
Imagine decorators like that:
@from_object(Project)
@owner_required
@as_html('projects/project.html')
def my_view(request, instance):
# do some useful stuff
return a context dict
with:
- from_object which deals with arguments and return an instance
- owner_required which verifies if the request.user is the owner of the retrieved object
- as_html which renders the context dict returned as an http response
You can of course set a lot of as_* decorators (as_json, as_rdfxml and so on) applied in urls in order to use the same view.
There is some basic code for now if you need to read code to better understand how it works (like me), do not hesitate to discuss.
This revision is from 2009-07-06 22:45