Class-Based Views and Django

May 19th, 2010

Here are the slides to the presentation on class-based views for Django that I gave at the Whatcom Python users group today. Be sure to check out the code that goes along with it, as well as the links for further reading at the end of the slides (especially Simon Willison's "Django Heresies").

Here is an example of using class based views straight out of our repo at WWU University Residences. This should give you an idea what views look like when you use classes.

# views.py

class Index(View):
    """Index view for RD Search."""
    template_name = "rdsearch/index.html"
    decorators = [decorate_method_with(login_required)]

    def get_context(self, request, *args, **kwargs):
        applicants = applicants_for_user(request.user.username)
        return {
            "applicant_stages": self._applicants_by_stage(applicants)
            }

    def _applicants_by_stage(self, applicants):
        stages = list(set([app["stage"] for app in applicants]))
        stages.sort()
        return [(pretty_stage(stage), applicants.filter(stage=stage))
                for stage in stages]

# urls.py

# Using "instantiator" to create new instances of the view class for each
# request. "instantiator" is located with the "View" class.

urlpatterns = patterns('',
    # ...
    url(r'^$', instantiator(Index), name="rdsearch_index"),
    # ...
)

« Previous Entry

Next Entry »

View Comments


Recent Entries


setTimeout Patterns in JavaScript

On August 10th, 2010


ParenScript is an Acceptable Lisp

On August 2nd, 2010


TryParenScript.com

On July 23rd, 2010


In response to "A JavaScript Function Guard"

On July 19th, 2010


My Notes from John Resig's "jQuery Hack Day" Talk

On July 5th, 2010


Announcing Pocco

On June 29th, 2010


Yet Another Lisp

On June 25th, 2010


Recent Happenings: All Play and No Work

On June 21st, 2010


Arguments.callee considered extraneous

On June 2nd, 2010


Javascript, "bind", and "this"

On May 20th, 2010


Creative Commons License

Fork me on GitHub