Hello, I'm Nick Fitzgerald. This is my weblog. You can also check out my shared items from Reader and code on GitHub. Feel free to contact me about whatever.
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"),
# ...
)
On August 10th, 2010
On August 2nd, 2010
On July 23rd, 2010
On July 19th, 2010
On July 5th, 2010
On June 29th, 2010
On June 25th, 2010
On June 21st, 2010
On June 2nd, 2010
On May 20th, 2010
blog comments powered by Disqus