ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Method-based views
in Django applications
      Gary Reynolds
     Touch Technology

           @goodtune
     bitbucket.org/goodtune
      github.com/goodtune
Refresher

? Last time we looked at building up a library
  which will help save us some boilerplate
? It incorporated better form ?elds, simple
  decorated mixins to enforce
  authentication, and a few other niceties
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(/slideshow/method-based-views-in-django-applications-14050952/14050952/r&),
    url(/slideshow/method-based-views-in-django-applications-14050952/14050952/r&),
)
Issues
? If your application requires con?guration 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.
Basic anatomy
class 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(/slideshow/method-based-views-in-django-applications-14050952/14050952/r&),
       )

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

   def index(self, request):
       return HttpResponse('')
Let¡¯s see it in practice
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.
Subclass our application
Real world example
? Tournament Control is an application for
  managing sporting competitions
 ? Scheduling ?xtures
 ? Allocating matches to grounds &
    timeslots
 ? Recording results and automatic ladder
    updates
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.
Method based views in django applications
Lets look at
             the front-end

www.touchsuperleague.org.uk/edinburgh/draws-ladders
Questions?
? Sample project can be obtained from
  bitbucket.org

 ? bitbucket.org/goodtune/sydjango-
    example-two

? This presentation can be downloaded from
  ºÝºÝߣShare

 ? slideshare.net/goodtune/method-based-
    views-in-django-applications-14050952

More Related Content

Method based views in django applications

  • 1. Method-based views in Django applications Gary Reynolds Touch Technology @goodtune bitbucket.org/goodtune github.com/goodtune
  • 2. Refresher ? Last time we looked at building up a library which will help save us some boilerplate ? It incorporated better form ?elds, simple decorated mixins to enforce authentication, and a few other niceties
  • 3. 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(/slideshow/method-based-views-in-django-applications-14050952/14050952/r&), url(/slideshow/method-based-views-in-django-applications-14050952/14050952/r&), )
  • 4. Issues ? If your application requires con?guration 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.
  • 5. Basic anatomy class 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(/slideshow/method-based-views-in-django-applications-14050952/14050952/r&), ) @property def urls(self): return self.get_urls(), self.app_name, self.name def index(self, request): return HttpResponse('')
  • 6. Let¡¯s see it in practice
  • 7. 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.
  • 9. Real world example ? Tournament Control is an application for managing sporting competitions ? Scheduling ?xtures ? Allocating matches to grounds & timeslots ? Recording results and automatic ladder updates
  • 10. 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.
  • 12. Lets look at the front-end www.touchsuperleague.org.uk/edinburgh/draws-ladders
  • 14. ? Sample project can be obtained from bitbucket.org ? bitbucket.org/goodtune/sydjango- example-two ? This presentation can be downloaded from ºÝºÝߣShare ? slideshare.net/goodtune/method-based- views-in-django-applications-14050952