$30 off During Our Annual Pro Sale. View Details »

Django Unchained

Django Unchained

Anil Shanbhag

January 22, 2013
Tweet

More Decks by Anil Shanbhag

Other Decks in Technology

Transcript

  1. Below is the workshop 'slides' for the event 'Django Unchained'

    orgainsed by WnCC, IIT Bombay on 23rd January, 2013. Django is a the most advanced and very popular Web Framework for developing webapps in Python. We shall follow the commands below through the course of the workshop c d m k d i r v i r t u a l e n v s c d v i r t u a l e n v s v i r t u a l e n v h e r o a p p c d h e r o a p p p i p i n s t a l l d j a n g o = = 1 . 4 No Django : Django Unchained What is Django? Setting up the Django Environment Installing Django Django Vs No-Django
  2. # ! / u s r / b i n

    / e n v p y t h o n i m p o r t M y S Q L d b p r i n t " C o n t e n t - T y p e : t e x t / h t m l \ n " p r i n t " < h t m l > < h e a d > < t i t l e > B o o k s < / t i t l e > < / h e a d > " p r i n t " < b o d y > " p r i n t " < h 1 > B o o k s < / h 1 > " p r i n t " < u l > " c o n n e c t i o n = M y S Q L d b . c o n n e c t ( u s e r = ' m e ' , p a s s w d = ' l e t m e i n ' , d b = ' m y _ d b ' ) c u r s o r = c o n n e c t i o n . c u r s o r ( ) c u r s o r . e x e c u t e ( " S E L E C T n a m e F R O M b o o k s O R D E R B Y p u b _ d a t e D E S C L I M I T 1 0 " ) f o r r o w i n c u r s o r . f e t c h a l l ( ) : p r i n t " < l i > % s < / l i > " % r o w [ 0 ] p r i n t " < / u l > " p r i n t " < / b o d y > < / h t m l > " c o n n e c t i o n . c l o s e ( ) Django : # m o d e l s . p y ( t h e d a t a b a s e t a b l e 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 c l a s s B o o k ( m o d e l s . M o d e l ) : 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 = 5 0 ) p u b _ d a t e = m o d e l s . D a t e F i e l d ( ) # v i e w s . p y ( t h e b u s i n e s s l o g i c )
  3. 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 _ t o _ r e s p o n s e f r o m m o d e l s i m p o r t B o o k d e f l a t e s t _ b o o k s ( r e q u e s t ) : b o o k _ l i s t = B o o k . o b j e c t s . o r d e r _ b y ( ' - p u b _ d a t e ' ) [ : 1 0 ] r e t u r n r e n d e r _ t o _ r e s p o n s e ( ' l a t e s t _ b o o k s . h t m l ' , { ' b o o k _ l i s t ' : b o o k _ l i s t } ) # u r l s . p y ( t h e U R L c o n f i g u r a t i o n ) f r o m d j a n g o . c o n f . u r l s . d e f a u l t s i m p o r t * 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 a t e s t / $ ' , v i e w s . l a t e s t _ b o o k s ) , ) # l a t e s t _ b o o k s . h t m l ( t h e t e m p l a t e ) < h t m l > < h e a d > < t i t l e > B o o k s < / t i t l e > < / h e a d > < b o d y > < h 1 > B o o k s < / h 1 > < u l > { % f o r b o o k i n b o o k _ l i s t % } < l i > { { b o o k . n a m e } } < / l i > { % e n d f o r % } < / u l > < / b o d y > < / h t m l > There is lot more structure and lesser dependency. Meaning I can change one part and get away without checking for side effects. 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 h e r o a p p Our New Project
  4. c d h e r o a p p /

    1. manage.py 2. settings.py 3. urls.py 4. wsgi.py Lets add some more files - Content will be added later 1. views.py 2. models.py p y t h o n m a n a g e . p y r u n s e r v e r # v i e w s . p y f r o m d j a n g o . h t t p i m p o r t H t t p R e s p o n s e d e f h e l l o ( r e q u e s t ) : r e t u r n H t t p R e s p o n s e ( " H e l l o w o r l d " ) # u r l s . p y f r o m d j a n g o . c o n f . u r l s . d e f a u l t s i m p o r t * f r o m h e r o a p p . v i e w s i m p o r t h e l l o u r l p a t t e r n s = p a t t e r n s ( ' ' , ( ' ^ h e l l o / $ ' , h e l l o ) , Understand the Structure Making of App - Step 0 - RunServer Making of App - Step 1 - View
  5. ) # v i e w s . p y

    f r o m d j a n g o . h t t p i m p o r t H t t p R e s p o n s e i m p o r t d a t e t i m e d e f h e l l o ( r e q u e s t ) : r e t u r n H t t p R e s p o n s e ( " H e l l o w o r l d " ) d e f c u r r e n t _ d a t e t i m e ( r e q u e s t ) : n o w = d a t e t i m e . d a t e t i m e . n o w ( ) h t m l = " < h t m l > < b o d y > I t i s n o w % s . < / b o d y > < / h t m l > " % n o w r e t u r n H t t p R e s p o n s e ( h t m l ) d e f h o u r s _ a h e a d ( r e q u e s t , o f f s e t ) : t r y : o f f s e t = i n t ( o f f s e t ) e x c e p t V a l u e E r r o r : r a i s e H t t p 4 0 4 ( ) d t = d a t e t i m e . d a t e t i m e . n o w ( ) + d a t e t i m e . t i m e d e l t a ( h o u r s = o f f s e t ) h t m l = " < h t m l > < b o d y > I n % s h o u r ( s ) , i t w i l l b e % s . < / b o d y > < / h t m l > " % ( o f f s e t , d t ) r e t u r n H t t p R e s p o n s e ( h t m l ) # u r l s . p y f r o m d j a n g o . c o n f . u r l s . d e f a u l t s i m p o r t * f r o m h e r o a p p . v i e w s i m p o r t h e l l o , c u r r e n t _ d a t e t i m e u r l p a t t e r n s = p a t t e r n s ( ' ' , ( ' ^ h e l l o / $ ' , h e l l o ) , ( ' ^ t i m e / $ ' , c u r r e n t _ d a t e t i m e ) , ( ' ^ a n o t h e r - t i m e - p a g e / $ ' , c u r r e n t _ d a t e t i m e ) , Making of App - Step 2 - Loose Coupling & Dynamic URL
  6. ( r ' ^ t i m e / p

    l u s / ( \ d + ) / $ ' , h o u r s _ a h e a d ) , ) Now comment out the try except block and see Django's beauty. Understand the Django Error Page - Boon for debugging. # h e r o t e m p l a t e . h t m l - > G o e s i n t o t e m p l a t e s / < h t m l > < h e a d > < t i t l e > O r d e r i n g n o t i c e < / t i t l e > < / h e a d > < b o d y > < h 1 > O r d e r i n g n o t i c e < / h 1 > < p > D e a r { { p e r s o n _ n a m e } } , < / p > < p > T h a n k s f o r p l a c i n g a n o r d e r f r o m { { c o m p a n y } } . I t ' s s c h e d u l e d t o s h i p o n { { s h i p _ d a t e | d a t e : " F j , Y " } } . < / p > < p > H e r e a r e t h e i t e m s y o u ' v e o r d e r e d : < / p > < u l > { % f o r i t e m i n i t e m _ l i s t % } < l i > { { i t e m } } < / l i > { % e n d f o r % } < / u l > { % i f o r d e r e d _ w a r r a n t y % } < p > Y o u r w a r r a n t y i n f o r m a t i o n w i l l b e i n c l u d e d i n t h e p a c k a g i n g . < / p > { % e l s e % } < p > Y o u d i d n ' t o r d e r a w a r r a n t y , s o y o u ' r e o n y o u r o w n w h e n t h e p r o d u c t s i n e v i t a b l y s t o p w o r k i n g . < / p > Making of App - Step 3 - Templates
  7. { % e n d i f % } <

    p > S i n c e r e l y , < b r / > { { c o m p a n y } } < / p > < / b o d y > < / h t m l > Now add templates dir into settings.py And finally setup the views file c = C o n t e x t ( { ' p e r s o n _ n a m e ' : ' J o h n S m i t h ' , ' c o m p a n y ' : ' O u t d o o r E q u i p m e n t ' , ' s h i p _ d a t e ' : d a t e t i m e . d a t e ( 2 0 0 9 , 4 , 2 ) , ' o r d e r e d _ w a r r a n t y ' : F a l s e } )