djangoic approach to implement common web development paradigms
- 1. Introduction Paradigms The Admin Interface The Django Advantage
Djangoic approach to implement common web
development paradigms
Lakshman Prasad
April 20, 2011
- 3. CS is neither about computers nor is science - Abelson, SICP, MIT
- 5. Introduction Paradigms The Admin Interface The Django Advantage
Why
- 6. Introduction Paradigms The Admin Interface The Django Advantage
• For building database driven web applications
- 7. Introduction Paradigms The Admin Interface The Django Advantage
• For building database driven web applications
• Emphasis on Reuse, DRY and simplicity
- 11. Introduction Paradigms The Admin Interface The Django Advantage
About Me
• Active Djangonaut and active in Python world
• Part of a few popular open source django applications
github.com/becomingGuru.
• Co-Authored an ebook ”django-design-patterns”
• Architect and develop django applications at InMobi
• Earlier, Consulting and Development via Agiliq Solutions
• Developed several custom proprietory django applications
• twitter.com/becomingGuru http://becomingguru.com
- 12. Introduction Paradigms The Admin Interface The Django Advantage
A nerd, for a while
- 16. Introduction Paradigms The Admin Interface The Django Advantage
Introduction
Definition
About Me
Abstractions
Paradigms
Forms
Authentication/Caching/Sessions
Inernational/Human/Local ..ization
Generic Views
Sorting/Pagination/DataBrowse/GRID/Messages/Other
The Admin Interface
Admin
Databrowse
The Django Advantage
Users
Community
Endnotes
- 28. Introduction Paradigms The Admin Interface The Django Advantage
Use a Model Form
>>> from d j a n g o . f o r m s import ModelForm
# C r e a t e t h e form c l a s s .
>>> c l a s s A r t i c l e F o r m ( ModelForm ) :
... c l a s s Meta :
... model = A r t i c l e
# C r e a t i n g a form t o add an a r t i c l e .
>>> form = A r t i c l e F o r m ( )
# C r e a t i n g a form t o c h an ge an e x i s t i n g a r t i c l e .
>>> a r t i c l e = A r t i c l e . o b j e c t s . g e t ( pk=1)
>>> form = A r t i c l e F o r m ( i n s t a n c e= a r t i c l e )
- 29. Introduction Paradigms The Admin Interface The Django Advantage
Save Foreign Keys
- 30. Introduction Paradigms The Admin Interface The Django Advantage
Use Model Formset
from d j a n g o . f o r m s . m o d e l s i m p o r t m o d e l f o r m s e t f a c t o r y
AuthorFormSet = m o d e l f o r m s e t f a c t o r y ( Author )
f o r m s e t = A ut h o r F o r m Se t ( )
> > print formset
>
<i n p u t t y p e=” h i d d e n ” name=” form− TOTAL FORMS”
v a l u e=” 1 ” i d=” i d f o r m − TOTAL FORMS” />
<i n p u t t y p e=” h i d d e n ” name=” form−INITIAL FORMS”
v a l u e=” 0 ” i d=” i d f o r m −INITIAL FORMS” />
<i n p u t t y p e=” h i d d e n ” name=” form− MAX NUM FORMS”
i d=” i d f o r m −MAX NUM FORMS” />
<t r > <th>
<l a b e l f o r=” i d f o r m −0 −name”>Name:</ l a b e l >
</th> <td>
<i n p u t i d=” i d f o r m −0 −name” t y p e=” t e x t ”
name=” form−0 −name” m a x l e n g t h=” 100 ” />
</td> </t r >
- 31. Introduction Paradigms The Admin Interface The Django Advantage
Model Formset options
f o r m s e t = AuthorFormSet ( q s=Au th o r . o b j e c t s . a l l ( ) ,
e x t r a =5)
formset . i s v a l i d ()
formset . e r r o r s
{ ’ name ’ : ’ T h i s f i e l d is required ’}
formset . changed forms
formset . save ()
- 32. Introduction Paradigms The Admin Interface The Django Advantage
Form pre populated
- 33. Introduction Paradigms The Admin Interface The Django Advantage
Forms dont have to save to a Model
from d j a n g o import f o r m s
c l a s s ContactForm ( f o r m s . Form ) :
s u b j e c t = f o r m s . C h a r F i e l d ( m a x l e n g t h =100)
message = f o r m s . C h a r F i e l d ( )
sender = forms . E m a i l F i e l d ()
c c m y s e l f = f o r m s . B o o l e a n F i e l d ( r e q u i r e d=F a l s e )
def s a v e ( s e l f ) :
#Do a n y t h i n g
...
- 35. Introduction Paradigms The Admin Interface The Django Advantage
Form Preview
from d j a n g o . c o n t r i b . f o r m t o o l s . p r e v i e w import FormPreview
from myapp . m o d e l s import SomeModel
#Add a u r l
( r ’ ˆ p o s t / $ ’ , SomeModelFormPreview ( SomeModelForm ) ) ,
#D e f i n e form p r e v i e w
c l a s s SomeModelFormPreview ( FormPreview ) :
def done ( s e l f , r e q u e s t , c l e a n e d d a t a ) :
# Do s o m e t h i n g w i t h t h e c l e a n e d d a t a , t h e n
# r e d i r e c t t o a ” s u c c e s s ” page .
r e t u r n H t t p R e s p o n s e R e d i r e c t ( ’ / form / s u c c e s s ’ )
- 37. Introduction Paradigms The Admin Interface The Django Advantage
Form Wizard
c l a s s C o n t a c t W i z a r d ( FormWizard ) :
def done ( s e l f , r e q u e s t , f o r m l i s t ) :
do something with the form data ( fo r m l is t )
return HttpResponseRedirect ( ’ / r e d i r e c t / ’ )
- 38. Introduction Paradigms The Admin Interface The Django Advantage
CSRF Protection
<form a c t i o n=” ” method=” p o s t ”>{% c s r f t o k e n %}
from d j a n g o . v i e w s . d e c o r a t o r s . c s r f import c s r f p r o t e c t
from d j a n g o . t e m p l a t e import R e q u e s t C o n t e x t
@csrf protect
def my view ( r e q u e s t ) :
c = {}
# ...
r e t u r n r e n d e r t o r e s p o n s e ( ” a t e m p l a t e . html ” , c ,
c o n t e x t i n s t a n c e=
RequestContext ( request ))
- 39. Introduction Paradigms The Admin Interface The Django Advantage
Authentication: Point to url pattern
u r l p a t t e r n s += p a t t e r n s ( ’ d j a n g o . c o n t r i b . a u t h . v i e w s ’ ,
u r l ( r ’ ˆ l o g i n /$ ’ , ’ l o g i n ’ ) ,
u r l ( r ’ ˆ l o g o u t /$ ’ , ’ l o g o u t ’ ) ,
u r l ( r ’ ˆ r e g i s t e r /$ ’ , ’ r e g i s t e r ’ ) ,
u r l ( r ’ ˆ p a s s r e s e t /$ ’ , ’ p a s s w o r d r e s e t ’ ) ,
u r l ( r ’ ˆ p a s s r e s e t 2 /$ ’ , ’ p a s s w o r d r e s e t d o n e ’ ) ,
u r l ( r ’ ˆ p a s s r e s e t c o n f i r m / ( ?P<u i d b 3 6 >[−w]+)/ ’ ,
’ password reset confirm ’ ) ,
u r l ( r ’ ˆ p a s s r e s e t c o m p l e t e /$ ’ ,
’ password reset complete ’ ) ,
)
- 40. Introduction Paradigms The Admin Interface The Django Advantage
Login Required
from d j a n g o . c o n t r i b . a u t h . d e c o r a t o r s import
login required
@ l o g i n r e q u i r e d ( r e d i r e c t f i e l d n a m e= ’ r e d i r e c t t o ’ )
def my view ( r e q u e s t ) :
...
- 41. Introduction Paradigms The Admin Interface The Django Advantage
Authentication backends
class SettingsBackend :
”””
A u t h e n t i c a t e a g a i n s t t h e s e t t i n g s ADMIN LOGIN
and ADMIN PASSWORD .
”””
def a u t h e n t i c a t e ( s e l f , username=None , p a s s w o r d=None )
i f l o g i n v a l i d and p w d v a l i d :
return user
r e t u r n None
def g e t u s e r ( s e l f , u s e r i d ) :
try :
r e t u r n U s e r . o b j e c t s . g e t ( pk=u s e r i d )
except U s e r . D o e s N o t E x i s t :
r e t u r n None
- 43. Introduction Paradigms The Admin Interface The Django Advantage
Open ID backend
c l a s s OpenIdBackend :
d e f a u t h e n t i c a t e ( s e l f , o p e n i d k e y , r e q u e s t , p r o v i d e r , u s e r=None ) :
try :
a s s o c = U s e r A s s o c i a t i o n . o b j e c t s . g e t ( o p e n i d k e y=o p e n i d k e y )
return assoc . user
except U s e r A s s o c i a t i o n . DoesNotExist :
#f e t c h i f o p e n i d p r o v i d e r p r o v i d e s any s i m p l e r e g i s t r a t i o n f i e l d s
i f r e q u e s t . o p e n i d and r e q u e s t . o p e n i d . s r e g :
email = request . openid . sreg . get ( ’ email ’ )
nickname = r e q u e s t . openid . s r e g . get ( ’ nickname ’ )
firstname , lastname = ( request . openid . sreg
. get ( ’ fullname ’ , ’ ’ )
return user
d e f GooglesAX ( s e l f , o p e n i d r e s p o n s e ) :
e m a i l = ( o p e n i d r e s p o n s e . ax
. g e t S i n g l e ( ’ h t t p : / / axschema . o r g / c o n t a c t / e m a i l ’ ) )
f i r s t n a m e = ( o p e n i d r e s p o n s e . ax
. g e t S i n g l e ( ’ h t t p : / / axschema . o r g / namePerson / f i r s t ’ ) )
l a s t n a m e = ( o p e n i d r e s p o n s e . ax
. g e t S i n g l e ( ’ h t t p : / / axschema . o r g / namePerson / l a s t ’ ) )
return l o c a l s ()
def g e t u s e r ( s e l f , u s e r i d ) :
try :
u s e r = U s e r . o b j e c t s . g e t ( pk=u s e r i d )
return user
except User . DoesNotExist :
r e t u r n None
- 45. Introduction Paradigms The Admin Interface The Django Advantage
Different blocks need different caching durations
- 46. Introduction Paradigms The Admin Interface The Django Advantage
Generally Memcache Every page
CACHE BACKEND = ’ memcached : / / 1 2 7 . 0 . 0 . 1 : 1 1 2 1 1 / ’
CACHE BACKEND = ’ memcached : / / 1 7 2 . 1 9 . 2 6 . 2 4 0 : 1 1 2 1 1 ;
172.19.26.242:11212;172.19.26.244:11213/ ’
- 47. Introduction Paradigms The Admin Interface The Django Advantage
Implement a caching decorator
from d j a n g o . c o r e . c a c h e import c a c h e
def c a c h e f o r ( s e c o n d s , f e t c h =0):
def c a c h e i t ( f u n c ) :
def d e c o f u n c ( s e l f ) :
v a l = cache . get ( s e l f . g e t c a c h e k e y ( f e t c h ))
i f not v a l :
val = func ( s e l f )
cache . s e t ( s e l f . g e t c a c h e k e y ( f e t c h ) ,
val , seconds )
return v a l
return deco func
return c a c h e i t
- 48. Introduction Paradigms The Admin Interface The Django Advantage
Apply on the blocks
class TwitterBlock ( TwitterSearchBlock ) :
t e m p l a t e n a m e = ’ t w i t t e r u s e r b l o c k . html ’
a j a x t e m p l a t e = ’ t w e e t s u s e r . html ’
@ c a c h e f o r ( 6 0 ∗ 6 0 , f e t c h =1)
def f e t c h d a t a ( s e l f ) :
tw = T w i t t e r ( e m a i l= ’ umoja com ’ )
u s e r t w e e t s = tw . s t a t u s e s . u s e r t i m e l i n e (
s c r e e n n a m e= s e l f . d a t a )
r e t u r n u s e r t w e e t s , None
- 49. Introduction Paradigms The Admin Interface The Django Advantage
Different cache Backends
• Database
• File System
• Local Memory
• Redis, a NoSQL store
• Dummy- For Development
• Sessions can use any of these, or the database.
- 50. Introduction Paradigms The Admin Interface The Django Advantage
Humanization and Localization
19 Apr 2 0 1 0 | n a t u r a l d a y becomes ‘ y e s t e r d a y ‘ .
20 Apr 2 0 1 0 | n a t u r a l d a y becomes ‘ today ‘ .
1 | o r d i n a l becomes ‘ 1 s t ‘ .
2 | o r d i n a l becomes ‘ 2 nd ‘ .
1 0 0 0 0 0 0 | i n t w o r d becomes ‘ 1 . 0 m i l l i o n ‘ .
1 2 0 0 0 0 0 | i n t w o r d becomes ‘ 1 . 2 m i l l i o n ‘ .
{{ v a l u e | l o c a l i z e }}
- 51. Introduction Paradigms The Admin Interface The Django Advantage
Internationalization
{% b l o c k t r a n s w i t h amount= a r t i c l e . p r i c e %}
That w i l l c o s t $ {{ amount } } .
{% e n d b l o c k t r a n s %}
{% b l o c k t r a n s w i t h myvar=v a l u e | f i l t e r %}
T h i s w i l l ha ve {{ myvar }} i n s i d e .
{% e n d b l o c k t r a n s %}
c l a s s in . forms . I N S t a t e F i e l d
c l a s s in . forms . INZipCodeField
c l a s s in . forms . I N S t a t e S e l e c t
- 52. Introduction Paradigms The Admin Interface The Django Advantage
Using Generic Views
# some app / v i e w s . py
from d j a n g o . v i e w s . g e n e r i c import TemplateView
c l a s s AboutView ( TemplateView ) :
t e m p l a t e n a m e = ” a b o u t . html ”
# u r l s . py
from d j a n g o . c o n f . u r l s . d e f a u l t s import ∗
from some app . v i e w s import AboutView
urlpatterns = patterns ( ’ ’ ,
( r ’ ˆ a b o u t / ’ , AboutView . a s v i e w ( ) ) ,
)
- 53. Introduction Paradigms The Admin Interface The Django Advantage
List and Object Views
from d j a n g o . v i e w s . g e n e r i c import L i s t V i e w
from b o o k s . m o d e l s import Book
c l a s s AcmeBookListView ( L i s t V i e w ) :
context object name = ” book list ”
q u e r y s e t = Book . o b j e c t s . f i l t e r ( p u b l i s h e r n a m e=
”Acme P u b l i s h i n g ” )
t e m p l a t e n a m e = ” b o o k s / a c m e l i s t . html ”
- 54. Introduction Paradigms The Admin Interface The Django Advantage
Mixins
from d j a n g o i m p o r t h t t p
from d j a n g o . u t i l s i m p o r t s i m p l e j s o n a s j s o n
c l a s s JSONResponseMixin ( o b j e c t ) :
def r e n d e r t o r e s p o n s e ( s e l f , context ) :
” R e t u r n s a JSON r e s p o n s e c o n t a i n i n g ’ c o n t e x t ’ a s p a y l o a d ”
return s e l f . get json response ( s e l f . convert context to json ( context ))
d e f g e t j s o n r e s p o n s e ( s e l f , c o n t e n t , ∗∗ h t t p r e s p o n s e k w a r g s ) :
” C o n s t r u c t an ‘ H t t p R e s p o n s e ‘ o b j e c t . ”
return http . HttpResponse ( content ,
c o n t e n t t y p e= ’ a p p l i c a t i o n / j s o n ’ ,
∗∗ h t t p r e s p o n s e k w a r g s )
def c o n v e r t c o n t e x t t o j s o n ( s e l f , context ) :
” C o n v e r t t h e c o n t e x t d i c t i o n a r y i n t o a JSON o b j e c t ”
r e t u r n j s o n . dumps ( c o n t e x t )
- 55. Introduction Paradigms The Admin Interface The Django Advantage
Any combinations of Mixins
c l a s s H y b r i d D e t a i l V i e w ( JSONResponseMixin ,
SingleObjectTemplateResponseMixin ,
BaseDetailView ) :
def r e n d e r t o r e s p o n s e ( s e l f , context ) :
# Look f o r a ’ f o r m a t=j s o n ’ GET argument
i f s e l f . r e q u e s t . GET . g e t ( ’ f o r m a t ’ , ’ h t m l ’ ) == ’ j s o n ’ :
r e t u r n JSONResponseMixin . r e n d e r t o r e s p o n s e ( s e l f , c o n t e x t )
else :
return SingleObjectTemplateResponseMixin . render to response ( s e l f ,
context )
- 56. Introduction Paradigms The Admin Interface The Django Advantage
Types of Generic Views
• Simple: Redirect, Render to Template
• Detail, List View
• Archive View: Date, Month, Year views
• Basic CRUD, with or w/o Auth
- 57. Introduction Paradigms The Admin Interface The Django Advantage
Builtin support for Pagination
>>> from d j a n g o . c o r e . p a g i n a t o r import P a g i n a t o r
>>> o b j e c t s = [ ’ j o h n ’ , ’ p a u l ’ , ’ g e o r g e ’ , ’ r i n g o ’ ]
>>> p = P a g i n a t o r ( o b j e c t s , 2 )
>>> p . count
4
>>> p . num pages
2
>>> p . page range
[1 , 2]
>>> page1 = p . page ( 1 )
>>> page1
<Page 1 o f 2>
>>> page1 . o b j e c t l i s t
[ ’ john ’ , ’ paul ’ ]
- 58. Introduction Paradigms The Admin Interface The Django Advantage
Abstract the pagination in middleware
django pagenation
{% a u t o p a g i n a t e o b j e c t l i s t 10 %}
{% p a g i n a t e %}
- 60. Introduction Paradigms The Admin Interface The Django Advantage
Other common paradigms
• Display Image Thumbnail
• Store data on a CDN, like Amazon S3
• Periodic tasks with Celery
• Decoupled design using Signals
• GeoDjango : Framework for all location based applications
- 62. Introduction Paradigms The Admin Interface The Django Advantage
Admin by models alone
- 63. Introduction Paradigms The Admin Interface The Django Advantage
Admin Syntax
from d j a n g o . c o n t r i b import admin
from m o d e l s import Post , Comment
c l a s s PostAdmin ( admin . ModelAdmin ) :
l i s t d i s p l a y = ( ’ t i t l e ’ , ’ datetime ’ )
c l a s s CommentAdmin ( admin . ModelAdmin ) :
l i s t d i s p l a y = ( ’ text ’ ,)
admin . s i t e . r e g i s t e r ( Post , PostAdmin )
admin . s i t e . r e g i s t e r ( Comment , CommentAdmin )
- 67. Introduction Paradigms The Admin Interface The Django Advantage
Databrowse enables browsable web complete with
hyperlinks
- 68. Introduction Paradigms The Admin Interface The Django Advantage
Popular Users
• Media
• LA Times
• NY Times
• Washington Post
• Guardian
• Hindustan TImes
- 69. Introduction Paradigms The Admin Interface The Django Advantage
Popular Users
• Media
• LA Times
• NY Times
• Washington Post
• Guardian
• Hindustan TImes
• Web2.0
• Mahalo: 10 million Page views
• Many web startups: Convore, Lanyard, Everyblock,
GroupQuality,
- 71. Introduction Paradigms The Admin Interface The Django Advantage
NASA uses Django
After an extensive trade study, we selected Django ...
as the first and primary application environment for the
Nebula Cloud.
- 77. Introduction Paradigms The Admin Interface The Django Advantage
Image Attributions
h t t p : / /www . f l i c k r . com/ p h o t o s / t e j e d o r o d e l u z /3157690060/
h t t p : / /www . f l i c k r . com/ p h o t o s /23820645 @N05 /4287681570/
h t t p : / /www . f l i c k r . com/ p h o t o s / a i d a n j o n e s /3575000735/
http :// j a c o b i a n . org /
h t t p : / / s a n j u a n c o l l e g e . edu / l i b / i m a g e s / p h i l o s o p h y b r a i n . j p g
h t t p : / /www . f l i c k r . com/ p h o t o s / uhop /105062059/
h t t p : / / s 3 . amazonaws . com/memebox/ u p l o a d s /136/ e x p o n e n t i a l g r a p h 2 . j p g
h t t p : / / g e e k a n d p o k e . t y p e p a d . com/ g e e k a n d p o k e / i m a g e s /2008/06/03/ s e x p l 1 8 . j p g
h t t p : / /www . f l i c k r . com/ p h o t o s / go /253819/
h t t p : / / a r o u n d t h e s p h e r e . f i l e s . w o r d p r e s s . com /2009/05/ s w i s s −army−k n i f e . j p g
h t t p : / /www . f r e e f o t o . com/ i m a g e s /41/04/41 0 4 9− −Keep−L e f t w e b . j p g
−
h t t p : / /www . f l i c k r . com/ p h o t o s / o r i n r o b e r t j o h n /114430223/