Django class based views (Dutch Django meeting presentation)
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Django class based views (Dutch Django meeting presentation)

on

  • 1,927 views

 

Statistics

Views

Total Views
1,927
Views on SlideShare
1,924
Embed Views
3

Actions

Likes
3
Downloads
20
Comments
0

2 Embeds 3

https://www.linkedin.com 2
http://www.linkedin.com 1

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Django class based views (Dutch Django meeting presentation) Presentation Transcript

  • 1. Class based views (django 1.3)
  • 2. Reinout van Rees• Nelen & Schuurmans • Django • Geo stuff, graphs, loads of python libs • Water management• http://reinout.vanrees.org• @reinoutvanrees
  • 3. Great 1.3 feature: CBV (class based views)• Models are classes, so why not views?• Django 1.3+• Bad PR: “class based generic views”• Question: who uses generic views?
  • 4. Classes and objects: theory• “Encapsulation”: everything in one place instead of all over the place.• “Inheritance”: inherit and change/add a couple of things. Helps with reuse and DRY.
  • 5. Function based view exampledef _expensive_calculation(request): # Calculate something and cache the result. return somethingdef some_view(request): template = my_app/someview.html data = _expensive_calculation(request) return render_to_response( template, {data: data}, context_instance=RequestContext(request))
  • 6. Class based view exampleclass SomeView(TemplateView): template_name = my_app/someview.html def _expensive_calculation(self): # Calculate something and cache the result. # You can use self.request! return something def get_context_data(self, **kwargs): return {data: self._expensive_calculation()}
  • 7. Hooking up in urls.pyurlpatterns = patterns( ... url(r^function_based_view/$, my_app.views.some_view, name=function_based_view), url(r^class_based_view/$, my_app.views.SomeView.as_view(), name=class_based_view), ...
  • 8. Edit viewsclass ProcessFormView(View): """ A mixin that processes a form on POST. """ def get(self, request, *args, **kwargs): form_class = self.get_form_class() form = self.get_form(form_class) return self.render_to_response( self.get_context_data(form=form)) def post(self, request, *args, **kwargs): form_class = self.get_form_class() form = self.get_form(form_class) if form.is_valid(): return self.form_valid(form) else: return self.form_invalid(form)
  • 9. Behind the scenes• A whole lot of of “mixin classes”.• A huge stack of ‘em.• Quite numerous.• Mixed together.• Lots of places to look and debug.
  • 10. Documentation• Currently the code is a better source of documentation than the code.• So look in django/views/generic/*.py• Basic suggestion: find out how to use the one or two you use daily.
  • 11. Example• TemplateView (from base.py) • template_name • get_context_data()• FormView (from edit.py) • template_name • form_class • success_url
  • 12. Feature request• No more hand-made context dict• But an object• Basically: {‘view’: self}• => {{ view.number_of_kittens }}
  • 13. Questions? (And a Dutch advertisement I’m sneaking in while you’re all asking questions)• Wij bij Nelen & Schuurmans zoeken weer nieuwe collega’s :-)• 40 m/v bedrijf, 9 django.• Veel open source, pypi, sinds kort github.• Hartje Utrecht. Vlak bij een ijskraam.• Schiet me aan (of reinout@vanrees.org)