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

An Introduction to Django

Tyler Harms
November 10, 2013

An Introduction to Django

Django is a Python MVC web framework "that encourages rapid development and clean, pragmatic design." In this presentation, we'll explore how Django makes application development easy and fast by re-building the South Dakota Code Camp website. Python experience is not required.

Tyler Harms

November 10, 2013
Tweet

More Decks by Tyler Harms

Other Decks in Programming

Transcript

  1. "DJANGO IS A HIGH-LEVEL PYTHON WEB FRAMEWORK THAT ENCOURAGES RAPID

    DEVELOPMENT AND CLEAN, PRAGMATIC DESIGN." https://www.djangoproject.com/
  2. MONTY PYTHON Guido van Rossum spam & eggs Significant White

    Space i m p o r t t h i s T h e Z e n o f P y t h o n , b y T i m P e t e r s B e a u t i f u l i s b e t t e r t h a n u g l y . E x p l i c i t i s b e t t e r t h a n i m p l i c i t . S i m p l e i s b e t t e r t h a n c o m p l e x . C o m p l e x i s b e t t e r t h a n c o m p l i c a t e d . F l a t i s b e t t e r t h a n n e s t e d . S p a r s e i s b e t t e r t h a n d e n s e . R e a d a b i l i t y c o u n t s . . . .
  3. INSTALLING DJANGO $ p i p i n s t

    a l l d j a n g o $ d j a n g o - a d m i n . p y s t a r t p r o j e c t s d c c s d c c ├ ─ ─ m a n a g e . p y └ ─ ─ s d c c ├ ─ ─ _ _ i n i t _ _ . p y ├ ─ ─ s e t t i n g s . p y ├ ─ ─ u r l s . p y └ ─ ─ w s g i . p y
  4. MANAGE.PY $ m a n a g e . p

    y < c o m m a n d > [ o p t i o n s ]
  5. SYNCDB $ p y t h o n m a

    n a g e . p y s y n c d b C r e a t i n g t a b l e s . . . C r e a t i n g t a b l e a u t h _ p e r m i s s i o n C r e a t i n g t a b l e a u t h _ g r o u p _ p e r m i s s i o n s C r e a t i n g t a b l e a u t h _ g r o u p C r e a t i n g t a b l e a u t h _ u s e r _ g r o u p s C r e a t i n g t a b l e a u t h _ u s e r _ u s e r _ p e r m i s s i o n s C r e a t i n g t a b l e a u t h _ u s e r C r e a t i n g t a b l e d j a n g o _ c o n t e n t _ t y p e C r e a t i n g t a b l e d j a n g o _ s e s s i o n C r e a t i n g t a b l e d j a n g o _ s i t e
  6. RUNSERVER $ p y t h o n m a

    n a g e . p y r u n s e r v e r V a l i d a t i n g m o d e l s . . . 0 e r r o r s f o u n d N o v e m b e r 0 9 , 2 0 1 3 - 1 3 : 2 9 : 0 7 D j a n g o v e r s i o n 1 . 5 . 5 , u s i n g s e t t i n g s ' s d c c . s e t t i n g s ' D e v e l o p m e n t s e r v e r i s r u n n i n g a t h t t p : / / 1 2 7 . 0 . 0 . 1 : 8 0 0 0 / Q u i t t h e s e r v e r w i t h C O N T R O L - C . http://localhost:8000
  7. STARTAPP python manage.py startapp <app_name> $ p y t h

    o n m a n a g e . p y s t a r t a p p c a m p s p e a k e r s ├ ─ ─ c a m p s p e a k e r s │ ├ ─ ─ _ _ i n i t _ _ . p y │ ├ ─ ─ m o d e l s . p y │ ├ ─ ─ t e s t s . p y │ └ ─ ─ v i e w s . p y ├ ─ ─ m a n a g e . p y └ ─ ─ s d c c ├ ─ ─ _ _ i n i t _ _ . p y ├ ─ ─ s e t t i n g s . p y ├ ─ ─ u r l s . p y ├ ─ ─ w s g i . p y
  8. Model = Model (ORM) View = Controller Template = View

    MODEL-VIEW-TEMPLATE Weird nameing convention, but follows M-V-C design pattern.
  9. MODELS f r o m d j a n g

    o . d b i m p o r t m o d e l s c l a s s S p e a k e r ( m o d e l s . M o d e l ) : f i r s t _ n a m e = m o d e l s . C h a r F i e l d ( m a x _ l e n g t h = 2 0 0 ) l a s t _ n a m e = m o d e l s . C h a r F i e l d ( m a x _ l e n g t h = 2 0 0 ) s l u g = m o d e l s . S l u g F i e l d ( u n i q u e = T r u e ) c l a s s M e t a : o r d e r i n g = [ ' l a s t _ n a m e ' ] d e f _ g e t _ f u l l _ n a m e ( s e l f ) : r e t u r n ' % s % s ' % ( s e l f . f i r s t _ n a m e , s e l f . l a s t _ n a m e ) f u l l _ n a m e = p r o p e r t y ( _ g e t _ f u l l _ n a m e ) d e f _ _ u n i c o d e _ _ ( s e l f ) : r e t u r n s e l f . f u l l _ n a m e
  10. DJANGO ADMIN 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 # U n c o m m e n t t h e n e x t t w o l i n e s t o e n a b l e t h e a d m i n : 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 ( ' ' , # E x a m p l e s : # u r l ( r ' ^ $ ' , ' s d c c . v i e w s . h o m e ' , n a m e = ' h o m e ' ) , # u r l ( r ' ^ s d c c / ' , i n c l u d e ( ' s d c c . f o o . u r l s ' ) ) , # U n c o m m e n t t h e a d m i n / d o c l i n e b e l o w t o e n a b l e a d m i n d o c u m e n t a t i o n : # u r l ( r ' ^ a d m i n / d o c / ' , i n c l u d e ( ' d j a n g o . c o n t r i b . a d m i n d o c s . u r l s ' ) ) , # U n c o m m e n t t h e n e x t l i n e t o e n a b l e t h e a d m i n : 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 ) ) , )
  11. DJANGO ADMIN 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 f r o m . m o d e l s i m p o r t S p e a k e r a d m i n . s i t e . r e g i s t e r ( S p e a k e r )
  12. DJANGO ADMIN Activate the Admin App I N S T

    A L L E D _ A P P S = ( ' d j a n g o . c o n t r i b . a u t h ' , ' d j a n g o . c o n t r i b . c o n t e n t t y p e s ' , ' d j a n g o . c o n t r i b . s e s s i o n s ' , ' d j a n g o . c o n t r i b . s i t e s ' , ' d j a n g o . c o n t r i b . m e s s a g e s ' , ' d j a n g o . c o n t r i b . s t a t i c f i l e s ' , # U n c o m m e n t t h e n e x t l i n e t o e n a b l e t h e a d m i n : ' d j a n g o . c o n t r i b . a d m i n ' , # U n c o m m e n t t h e n e x t l i n e t o e n a b l e a d m i n d o c u m e n t a t i o n : # ' d j a n g o . c o n t r i b . a d m i n d o c s ' , )
  13. VIEWS Functional Based Views f r o m d j

    a n g o . s h o r t c u t s i m p o r t r e n d e r f r o m d j a n g o . h t t p i m p o r t H t t p 4 0 4 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 . m o d e l s i m p o r t S p e a k e r d e f i n d e x ( r e q u e s t ) : t r y : s p e a k e r _ l i s t = S p e a k e r . o b j e c t s . a l l ( ) e x c e p t S p e a k e r . D o e s N o t E x i s t : r a i s e H t t p 4 0 4 r e t u r n r e n d e r ( r e q u e s t , ' c a m p s p e a k e r s / s p e a k e r _ l i s t . h t m l ' , { ' s p e a k e r _ l i s t ' : s p e a k e r _ l i s t } )
  14. VIEWS Class Based Views Different Class Based Views exist for

    different tasks f r o m d j a n g o . v i e w s . g e n e r i c i m p o r t T e m p l a t e V i e w c l a s s I n d e x V i e w ( T e m p l a t e V i e w ) : t e m p l a t e _ n a m e = " c a m p s p e a k e r s / i n d e x . h t m l "
  15. VIEWS Class Based Views cont. f r o m d

    j a n g o . v i e w s . g e n e r i c i m p o r t L i s t V i e w f r o m . m o d e l s i m p o r t S p e a k e r c l a s s B o o k L i s t V i e w ( L i s t V i e w ) : m o d e l = S p e a k e r d e f g e t _ c o n t e x t _ d a t a ( s e l f , * * k w a r g s ) : # C a l l t h e b a s e i m p l e m e n t a t i o n f i r s t t o g e t a c o n t e x t c o n t e x t = s u p e r ( B o o k L i s t V i e w , s e l f ) . g e t _ c o n t e x t _ d a t a ( * * k w a r g s ) # A d d i n a Q u e r y S e t o f a l l t h e b o o k s c o n t e x t [ ' s p e a k e r _ l i s t ' ] = S p e a k e r . o b j e c t s . a l l ( ) r e t u r n c o n t e x t
  16. ROUTES 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 # U n c o m m e n t t h e n e x t t w o l i n e s t o e n a b l e t h e a d m i n : 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 ( ' ' , # E x a m p l e s : # u r l ( r ' ^ $ ' , ' s d c c . v i e w s . h o m e ' , n a m e = ' h o m e ' ) , # u r l ( r ' ^ s d c c / ' , i n c l u d e ( ' s d c c . f o o . u r l s ' ) ) , u r l ( r ' ^ s p e a k e r s / ' , i n c l u d e ( ' c a m p s p e a k e r s . u r l s ' ) ) , # U n c o m m e n t t h e a d m i n / d o c l i n e b e l o w t o e n a b l e a d m i n d o c u m e n t a t i o n : # u r l ( r ' ^ a d m i n / d o c / ' , i n c l u d e ( ' d j a n g o . c o n t r i b . a d m i n d o c s . u r l s ' ) ) , # U n c o m m e n t t h e n e x t l i n e t o e n a b l e t h e a d m i n : 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 ) ) , )
  17. ROUTES 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 . v i e w s i m p o r t B o o k L i s t V i e w 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 ( ' ' , ( r ' ^ l i s t $ ' , B o o k L i s t V i e w . a s _ v i e w ( ) ) , u r l ( r ' ^ $ ' , v i e w s . i n d e x , n a m e = ' i n d e x ' ) , )
  18. TEMPLATES { % b l o c k c o

    n t e n t % } { % e n d b l o c k % } { { s p e a k e r } }
  19. SOUTH "This is South, intelligent schema and data migrations for

    ​ Django projects" Replaces syncdb. $ p y t h o n m a n a g e . p y s c h e m a m i g r a t i o n c a m p s p e a k e r s - - i n i t i a l $ p y t h o n m a n a g e . p y m i g r a t e c a m p s p e a k e r s
  20. DIRECTORY VARIABLES i m p o r t o s

    P R O J E C T _ D I R = o s . p a t h . d i r n a m e ( o s . p a t h . a b s p a t h ( _ _ f i l e _ _ ) ) B A S E _ D I R = o s . p a t h . a b s p a t h ( o s . p a t h . j o i n ( P R O J E C T _ D I R , ' . . / . . / ' ) )