Method based views in django applications

14

Click here to load reader

Transcript of Method based views in django applications

Page 1: Method based views in django applications

Method-based viewsin Django applications

Gary ReynoldsTouch Technology

@goodtunebitbucket.org/goodtunegithub.com/goodtune

Page 2: Method based views in django applications

Refresher

• Last time we looked at building up a library which will help save us some boilerplate

• It incorporated better form fields, simple decorated mixins to enforce authentication, and a few other niceties

Page 3: Method based views in django applications

This time

• What if we need to deploy the same application multiple times?

• In our URLconf we could point to the applications urls.py as many times as we need...

from django.conf.urls.defaults import patterns, url

urlpatterns = patterns('', url(r'^usual-use/', 'myapp.urls'), url(r'^it/makes/sense/here/too/', 'myapp.urls'),)

Page 4: Method based views in django applications

Issues

• If your application requires configuration at run-time, you probably use a custom settings.MYAPP_VAR or similar?

• That will usually apply globally, not per mount point.

• If the application was an instance with it’s own state, the issue goes away.

Page 5: Method based views in django applications

Basic anatomyclass MyApp(object):

def __init__(self, name='myapp', app_name='myapp'): self.name = name self.app_name = app_name

def get_urls(self): urlpatterns = patterns('', url(r'^$', self.index, name='index'), )

@property def urls(self): return self.get_urls(), self.app_name, self.name

def index(self, request): return HttpResponse('')

Page 6: Method based views in django applications

Let’s see it in practice

Page 7: Method based views in django applications

Horses for courses

• This book store is missing a view for the details of a book.

• It really is an admin application, allowing authenticated creation and updating of Book instances by authenticated users.

• Lets use inheritance to split the functionality.

Page 8: Method based views in django applications

Subclass our application

Page 9: Method based views in django applications

Real world example

• Tournament Control is an application for managing sporting competitions

• Scheduling fixtures

• Allocating matches to grounds & timeslots

• Recording results and automatic ladder updates

Page 10: Method based views in django applications

Tournament Control• Shameless plug

• http://www.sydney.touch.asn.au/

• http://www.touchsuperleague.org.uk/

• http://www.touchworldcup2011.co.uk/

• We’re interested in Touch Superleague because they run multiple application instances - they have venues in Edinburgh, Cardiff, and Jersey.

Page 11: Method based views in django applications
Page 12: Method based views in django applications

Lets look atthe front-end

www.touchsuperleague.org.uk/edinburgh/draws-ladders

Page 13: Method based views in django applications

Questions?