Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ConvergeSE - Intro to Django

ConvergeSE - Intro to Django

gaker

May 02, 2014
Tweet

More Decks by gaker

Other Decks in Programming

Transcript

  1. Me! 1. Developer at nGen Works 2. SysAdmin at Arcustech

    3. Former ExpressionEngine core dev 4. Accidentally became a developer at 28
  2. What is Django? Started as an in-house CMS at the

    Lawrence Journal-World Released in 2005 with a BSD License Project managed by the Django Software Foundation
  3. What is Django? High-Level Web Framework Follows a Model-View-Controller pattern

    Has a beautiful template syntax language Wonderful documentation Vibrant open-source community
  4. Use Cases Want total control over you data modeling Custom

    web applications Need to scale Build first-party APIs
  5. PostgreSQL Powerful relational database Used by Rdio, Instagram, and many

    others Django also supports MySQL, Oracle and SQLite
  6. Install PIP $ s u d o e a s

    y _ i n s t a l l p i p
  7. Install Virtualenv $ s u d o p i p

    i n s t a l l v i r t u a l e n v Create virtualenv $ v i r t u a l e n v e n v "Activate" the virtualenv $ s o u r c e e n v / b i n / a c t i v a t e
  8. Python Crash Course Tabs and white space are important (4

    spaces to a tab. Always) Tuple: a = ( 1 , 2 , 3 , 4 , 5 ) - (Immutable) List: a = [ 1 , 2 , 3 , 4 , 5 ] - (Mutable) Dictionary: a = { ' l i f e ' : 4 2 , ' b ' : [ 1 ] } - (JSON!)
  9. Crash Course (cont'd) Function d e f m y _

    f u n c ( ) : r e t u r n T r u e
  10. Crash Course (cont'd) Classes c l a s s M

    y C l a s s ( o b j e c t ) : " " " T h i s i s a c o m m e n t a b o u t t h e c l a s s . " " " d e f s o m e _ s t r i n g ( s e l f ) : r e t u r n ' a s t r i n g ' d e f s o m e _ i n t e g e r ( s e l f ) : r e t u r n 1 d e f s o m e _ b o o l e a n ( s e l f ) : r e t u r n F a l s e
  11. Crash Course (cont'd) c l a s s B a

    d M a t h ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f , n u m b e r ) : s e l f . n u m b e r = n u m b e r d e f a d d _ o n e ( s e l f ) : s e l f . n u m b e r = s e l f . n u m b e r + 1 r e t u r n s e l f . n u m b e r d e f m i n u s _ o n e ( s e l f ) : s e l f . n u m b e r = s e l f . n u m b e r - 1 r e t u r n s e l f . n u m b e r
  12. Anatomy of a Django Project . ├ ─ ─ c

    o m p a n y │ ├ ─ ─ _ _ i n i t _ _ . p y │ ├ ─ ─ a p p s │ │ ├ ─ ─ _ _ i n i t _ _ . p y │ │ └ ─ ─ b l o g │ │ ├ ─ ─ _ _ i n i t _ _ . p y │ │ ├ ─ ─ a d m i n . p y │ │ ├ ─ ─ m i g r a t i o n s │ │ ├ ─ ─ m o d e l s . p y │ │ ├ ─ ─ t e s t s . p y │ │ ├ ─ ─ u r l s . p y │ │ └ ─ ─ v i e w s . p y │ ├ ─ ─ s e t t i n g s . p y │ ├ ─ ─ s t a t i c │ ├ ─ ─ t e m p l a t e s │ ├ ─ ─ u r l s . p y │ └ ─ ─ w s g i . p y ├ ─ ─ m a n a g e . p y └ ─ ─ r e q u i r e m e n t s . p i p
  13. Install Requirements $ p i p i n s t

    a l l - r r e q u i r e m e n t s . p i p File name isn't standardized. requirements.txt, dependencies.txt, .pip, etc
  14. Syncdb & Migrate $ p y t h o n

    m a n a g e . p y s y n c d b - - m i g r a t e Make sure the virtualenv activated Run every time you pull from source control
  15. Built in Web Server $ p y t h o

    n m a n a g e . p y r u n s e r v e r Starts a local web server on 1 2 7 . 0 . 0 . 1 : 8 0 0 0
  16. Models Python Class Subclass of d j a n g

    o . d b . m o d e l s . M o d e l Represents your data and makes queries to the database.
  17. Example f r o m d j a n g

    o . c o n f i m p o r t s e t t i n g s f r o m d j a n g o . d b i m p o r t m o d e l s f r o m d j a n g o . u t i l s . t i m e z o n e i m p o r t n o w c l a s s P o s t ( m o d e l s . M o d e l ) : " " " B l o g P o s t M o d e l " " " t i t l e = m o d e l s . C h a r F i e l d ( ' t i t l e ' , m a x _ l e n g t h = 1 0 0 ) s l u g = m o d e l s . S l u g F i e l d ( ' s l u g ' , m a x _ l e n g t h = 1 0 0 ) b o d y = m o d e l s . T e x t F i e l d ( ' p o s t b o d y ' , h e l p _ t e x t = " W r i t e y o u r p o s t h e r e . " ) d a t e _ c r e a t e d = m o d e l s . D a t e T i m e F i e l d ( ' d a t e c r e a t e d ' , d e f a u l t = n o w ) p u b l i s h e d = m o d e l s . B o o l e a n F i e l d ( ' p u b l i s h e d ' , d e f a u l t = T r u e ) a u t h o r = m o d e l s . F o r e i g n K e y ( s e t t i n g s . A U T H _ U S E R _ M O D E L , b l a n k = T r u e , n u l l = T r u e )
  18. Translates to T a b l e " p u

    b l i c . b l o g _ p o s t " C o l u m n | T y p e | M o d i f i e r s - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - i d | i n t e g e r | n o t n u l l d e f a u l t n e x t v a l ( ' b l o g _ p o s t _ i d _ s e q ' : : r e g c l a s s ) t i t l e | c h a r a c t e r v a r y i n g ( 1 0 0 ) | n o t n u l l s l u g | c h a r a c t e r v a r y i n g ( 1 0 0 ) | n o t n u l l b o d y | t e x t | n o t n u l l d a t e _ c r e a t e d | t i m e s t a m p w i t h t i m e z o n e | n o t n u l l p u b l i s h e d | b o o l e a n | n o t n u l l a u t h o r _ i d | i n t e g e r | I n d e x e s : " b l o g _ p o s t _ p k e y " P R I M A R Y K E Y , b t r e e ( i d ) " b l o g _ p o s t _ a u t h o r _ i d " b t r e e ( a u t h o r _ i d ) " b l o g _ p o s t _ s l u g " b t r e e ( s l u g ) " b l o g _ p o s t _ s l u g _ l i k e " b t r e e ( s l u g v a r c h a r _ p a t t e r n _ o p s ) F o r e i g n - k e y c o n s t r a i n t s : " a u t h o r _ i d _ r e f s _ i d _ d a a a 0 e 9 0 " F O R E I G N K E Y ( a u t h o r _ i d ) R E F E R E N C E S a u t h _ u s e r ( i d ) D E F E R R A B L E I N I T I A L L Y D E F E R R E D
  19. URLs & Views URLs are configured in u r l

    s . p y There is no automatic routing Total control on URL construction!
  20. Example u r l s . p y f r

    o m d j a n g o . c o n f i m p o r t s e t t i n g s f r o m d j a n g o . c o n f . u r l s i m p o r t p a t t e r n s , i n c l u d e , u r l f r o m d j a n g o . c o n f . u r l s . s t a t i c i m p o r t s t a t i c f r o m d j a n g o . c o n t r i b i m p o r t a d m i n a d m i n . a u t o d i s c o v e r ( ) u r l p a t t e r n s = p a t t e r n s ( ' ' , u r l ( r ' ^ ' , i n c l u d e ( ' c o m p a n y . a p p s . b l o g . u r l s ' ) ) , u r l ( r ' ^ a d m i n / ' , i n c l u d e ( a d m i n . s i t e . u r l s ) ) , ) i f s e t t i n g s . D E B U G a n d s e t t i n g s . M E D I A _ R O O T : u r l p a t t e r n s + = s t a t i c ( s e t t i n g s . M E D I A _ U R L , d o c u m e n t _ r o o t = s e t t i n g s . M E D I A _ R O O T )
  21. Blog URLs f r o m d j a n

    g o . c o n f . u r l s i m p o r t p a t t e r n s , u r l f r o m c o m p a n y . a p p s . b l o g i m p o r t v i e w s u r l p a t t e r n s = p a t t e r n s ( ' ' , u r l ( r ' ^ $ ' , v i e w s . P o s t L i s t . a s _ v i e w ( ) , n a m e = ' b l o g - l i s t ' ) , u r l ( r ' ^ ( ? P < y e a r > \ d { 4 } ) / ( ? P < m o n t h > \ w { 3 } ) / ( ? P < d a y > \ d { 2 } ) / ( ? P < s l u g > [ - \ w ] + ) / $ ' , v i e w s . P o s t D e t a i l . a s _ v i e w ( ) , n a m e = ' b l o g - d e t a i l ' ) , )
  22. Generic Views Template View (no auto DB functionality) List (provides

    pagination) Detail (provides 404) Edit/Create/Delete
  23. Views c o m p a n y / a

    p p s / b l o g / v i e w s . p y f r o m d j a n g o . v i e w s i m p o r t g e n e r i c f r o m c o m p a n y . a p p s . b l o g . m o d e l s i m p o r t P o s t c l a s s P o s t L i s t ( g e n e r i c . L i s t V i e w ) : " " " L i s t o f p o s t s " " " t e m p l a t e _ n a m e = ' b l o g / l i s t . h t m l ' q u e r y s e t = P o s t . o b j e c t s . f i l t e r ( p u b l i s h e d = T r u e ) . s e l e c t _ r e l a t e d ( ' a u t h o r ' ) c o n t e x t _ o b j e c t _ n a m e = ' p o s t s ' c l a s s P o s t D e t a i l ( g e n e r i c . D e t a i l V i e w ) : " " " D e t a i l v i e w . " " " q u e r y s e t = P o s t . o b j e c t s . f i l t e r ( p u b l i s h e d = T r u e ) . s e l e c t _ r e l a t e d ( ' a u t h o r ' ) t e m p l a t e _ n a m e = ' b l o g / d e t a i l . h t m l ' c o n t e x t _ o b j e c t _ n a m e = ' p o s t '
  24. Inheritance b a s e . h t m l

    < ! D O C T Y P E h t m l > < h t m l l a n g = " e n " > < h e a d > < t i t l e > H i < / t i t l e > < / h e a d > < b o d y > { % b l o c k c o n t e n t % } { % e n d b l o c k % } < / b o d y > < / h t m l > b l o g / l i s t . h t m l { % e x t e n d s " b a s e . h t m l " % } { % b l o c k c o n t e n t % } < h 1 > H e l l o w o r l d ! < / h 1 > T h i s w i l l b e d r o p p e d i n t o t h e c o n t e n t b l o c k i n t h e b a s e t e m p l a t e { % e n d b l o c k % }
  25. Context Variables available in the template { % e x

    t e n d s " b a s e . h t m l " % } { % b l o c k c o n t e n t % } { % f o r p o s t i n p o s t s % } < h 1 > { { p o s t . t i t l e } } < / h 1 > { % i f p o s t . a u t h o r % } < s m a l l > P o s t e d b y : { { p o s t . a u t h o r } } < / s m a l l > { % e n d i f % } { { p o s t . b o d y | l i n e b r e a k s } } { % e n d f o r % } { % e n d b l o c k % }
  26. Context (cont'd) Django Debug Toolbar $ p i p i

    n s t a l l d j a n g o - d e b u g - t o o l b a r
  27. Filters Separated by pipes { { p o s t

    . b o d y | l i n e b r e a k s } } Could render < p > H e l l o w o r l d < / p >
  28. Filters (cont'd) { { p o s t . b

    o d y | l i n e b r e a k s | u r l i z e } } Could render < p > H e l l o w o r l d ! < a h r e f = " h t t p : / / w w w . g o o g l e . c o m " > w w w . g o o g l e . c o m < / a > < / p >
  29. Tags Constructed with { % t a g _ n

    a m e % } Single tag Tag Pair { % e x t e n d s " b a s e . h t m l " % } { % f o r p o s t i n p o s t s % } { { p o s t . t i t l e } } { % e n d f o r % } https://docs.djangoproject.com/en/dev/ref/templates/builtins/
  30. Custom Tags Written in Python Django TTag app makes it

    much easier $ p i p i n s t a l l d j a n g o - t t a g
  31. Tags (cont'd) c o m p a n y /

    a p p s / b l o g / t e m p l a t e t a g s / r e c e n t _ p o s t s . p y i m p o r t t t a g f r o m d j a n g o i m p o r t t e m p l a t e f r o m c o m p a n y . a p p s . b l o g . m o d e l s i m p o r t P o s t r e g i s t e r = t e m p l a t e . L i b r a r y ( ) c l a s s G e t R e c e n t P o s t s ( t t a g . h e l p e r s . A s T a g ) : d e f o u t p u t ( s e l f , d a t a ) : r e t u r n P o s t . o b j e c t s . a l l ( ) r e g i s t e r . t a g ( G e t R e c e n t P o s t s )
  32. Tags (cont'd) Custom Tag Usage { % e x t

    e n d s " b a s e . h t m l " % } { % l o a d r e c e n t _ p o s t s % } < s e c t i o n > { % c o m m e n t % } A b o u t u s o r r e c e n t w o r k , e t c h e r e { % e n d c o m m e n t % } < / s e c t i o n > < a s i d e c l a s s = " s i d e b a r " > { % g e t _ r e c e n t _ p o s t s a s r e c e n t _ p o s t s % } < h 2 > R e c e n t P o s t s < / h 2 > { # g e t t h r e e m o s t r e c e n t p o s t s # } { % f o r p o s t i n r e c e n t _ p o s t s | s l i c e : " : 3 " % } { { p o s t . t i t l e } } { % e n d f o r % } < / a s i d e >
  33. Example Display 3 posts by 3 authors { % f

    o r p o s t i n p o s t s % } { { p o s t . a u t h o r } } { % e n d f o r % } 6 queries
  34. Use s e l e c t _ r e

    l a t e d ( ) 6 queries down to 3
  35. Queries can get out of hand Avoid queries in loops

    Worst I've seen is 20k+ queries on a single page Forcing JOINs with s e l e c t _ r e l a t e d ( ) cut it down to 80 queries. Start to get nervous when you're over 50 queries on a single page load.
  36. Asset Management . ├ ─ ─ c o m p

    a n y │ ├ ─ ─ _ _ i n i t _ _ . p y │ ├ ─ ─ a p p s │ ├ ─ ─ s e t t i n g s . p y │ ├ ─ ─ s t a t i c │ ├ ─ ─ t e m p l a t e s │ ├ ─ ─ u r l s . p y │ └ ─ ─ w s g i . p y ├ ─ ─ m a n a g e . p y └ ─ ─ r e q u i r e m e n t s . p i p Directory location is configurable Put everything in "static"
  37. Stateless Easily Scale Horizontally Keep uploads on S3 or Rackspace

    CloudFiles Use a CDN for static assets (JS, CSS, App Images)
  38. Compress CSS/JS Django Compressor What do we Install it with?

    PIP! $ p i p i n s t a l l d j a n g o _ c o m p r e s s o r
  39. Example usage { % l o a d c o

    m p r e s s o r % } { % c o m p r e s s j s % } / / P u t y o u r j s f i l e s o r i n l i n e h e r e . { % e n d c o m p r e s s % } Can create a single file with a version number and minified JS. { % l o a d c o m p r e s s o r % } { % c o m p r e s s c s s % } / / P u t y o u r c s s f i l e s o r i n l i n e h e r e . { % e n d c o m p r e s s % }
  40. Where do I go from here? Django docs.djangoproject.com 2 Scoops

    of Django GettingStartedWithDjango.com Python LearnPythonTheHardWay.com