Introduction Forms Authentication Generic Views Caching Others Admin
Web Development Paradigms and how django
and GAE webapp approach them.
Lakshman Prasad
Agiliq SolutionsIntroduction Forms Authentication Generic Views Caching Others Admin
How do I know about web development or django
•
Active Djangonaut
•
Part of a few popular open source django applications
github.com/becomingGuru, github.com/agiliq
•
Consulting and Development via
•
Developed several custom proprietory django applications
Introduction Forms Authentication Generic Views Caching Others Admin
Introduction
Forms
Authentication
Generic Views
Caching
Others
Admin
Introduction Forms Authentication Generic Views Caching Others Admin
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 f o r m 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 f o r m t o add an a r t i c l e . >>> f o r m = A r t i c l e F o r m ( ) # C r e a t i n g a f o r m t o c h a n g e 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) >>> f o r m = 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 )
Introduction Forms Authentication Generic Views Caching Others Admin
Model Syntax
from d j a n g o . db import m o d e l s c l a s s P o s t ( m o d e l s . Model ) : t i t l 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 =100) t e x t = m o d e l s . T e x t F i e l d ( ) d a t e t i m e = m o d e l s . D a t e T i m e F i e l d ( ) c l a s s Meta : o r d e r i n g = ( ’−d a t e t i 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 . t i t l e c l a s s Comment ( m o d e l s . Model ) : p o s t = m o d e l s . F o r e i g n K e y ( P o s t ) t e x t = m o d e l s . T e x t F i e l d ( )Introduction Forms Authentication Generic Views Caching Others Admin
Model API
>>>from b l o g . m o d e l s import Post , Comment
>>>p o s t = P o s t . o b j e c t s . a l l ( ) [ 0 ]
Introduction Forms Authentication Generic Views Caching Others Admin
Introduction Forms Authentication Generic Views Caching Others Admin
Use Model Formset
from d j a n g o . f o r m s . m o d e l s import m o d e l f o r m s e t f a c t o r y A u t h o r F o r m S e t = m o d e l f o r m s e t f a c t o r y ( A u t h o r )
f o r m s e t = A u t h o r F o r m S e t ( )
>>> p r i n t f o r m s e t
<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>
Introduction Forms Authentication Generic Views Caching Others Admin
Model Formset options
f o r m s e t = A u t h o r F o r m S e t ( q s=A u t h o r . o b j e c t s . a l l ( ) , e x t r a =5) f o r m s e t . i s v a l i d ( ) f o r m s e t . e r r o r s {’ name ’ : ’ T h i s f i e l d i s r e q u i r e d ’} f o r m s e t . c h a n g e d f o r m s f o r m s e t . s a v e ( )
Introduction Forms Authentication Generic Views Caching Others Admin
Introduction Forms Authentication Generic Views Caching Others Admin
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 C o n t a c t F o r m ( 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) m e s s a g e = f o r m s . C h a r F i e l d ( ) s e n d e r = f o r m s . 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 ) d e f s a v e ( s e l f ) : #Do a n y t h i n g . . .
Introduction Forms Authentication Generic Views Caching Others Admin
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 F o r m P r e v i e w 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 f o r m p r e v i e w c l a s s SomeModelFormPreview ( F o r m P r e v i e w ) : d e f 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 ” p a g e . 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 ( ’ / f o r m / s u c c e s s ’ )
Introduction Forms Authentication Generic Views Caching Others Admin
Form Wizard
c l a s s C o n t a c t W i z a r d ( FormWizard ) : d e f done ( s e l f , r e q u e s t , f o r m l i s t ) : d o s o m e t h i n g w i t h t h e f o r m d a t a ( f o r m l i s t ) 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 ( ’ / page−to−r e d i r e c t−to−when−done / ’ )Introduction Forms Authentication Generic Views Caching Others Admin
Point to url pattern
u r l p a t t e r n s += p a t t e r n s ( ’ r e g i s t r a t i o n . v i e w s ’ , u r l ( r ’ ˆ l o g i n / $ ’ , a u t h v i e w s . l o g i n , name= ’ a u t h l o g i n ’ ) , u r l ( r ’ ˆ l o g o u t / $ ’ , a u t h v i e w s . l o g o u t , name= ’ a u t h 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 ’ , name= ’ r e g i s t e r v i e w ’ ) , u r l ( r ’ ˆ p a s s r e s e t / $ ’ , a u t h v i e w s . p a s s w o r d r e s e t , name= ’ f o r g o t p a s s w o r d 1 ’ ) , u r l ( r ’ ˆ p a s s r e s e t d o n e / $ ’ , a u t h v i e w s . p a s s w o r d r e s e t d o n e , name= ’ f o r g o t p a s s w o r d 2 ’ ) , 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] + ) / ( ? P<t o k e n>[−\w] + ) / $ ’ , a u t h v i e w s . p a s s w o r d r e s e t c o n f i r m , name= ’ f o r g o t p a s s w o r d 3 ’ ) , u r l ( r ’ ˆ p a s s r e s e t c o m p l e t e / $ ’ , a u t h v i e w s . p a s s w o r d r e s e t c o m p l e t e , name= ’ f o r g o t p a s s w o r d 4 ’ ) , )
Introduction Forms Authentication Generic Views Caching Others Admin
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 l o g i n r e q u i r e d @ 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 ’ ) d e f m y v i e w ( r e q u e s t ) : . . .Introduction Forms Authentication Generic Views Caching Others Admin
Authentication backends
from d j a n g o . c o n f import s e t t i n g s from d j a n g o . c o n t r i b . a u t h . m o d e l s import U s e r , c h e c k p a s s w o r d c l a s s S e t t i n g s B a c k e n d : ”””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 . ””” d e f a u t h e n t i c a t e ( s e l f , u s e r n a m e=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 : r e t u r n u s e r r e t u r n None d e f g e t u s e r ( s e l f , u s e r i d ) : t r y: 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 ) e x c e p t U s e r . D o e s N o t E x i s t : r e t u r n None
Introduction Forms Authentication Generic Views Caching Others Admin
Generic Views
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 d j a n g o . v i e w s . g e n e r i c . s i m p l e \ import d i r e c t t o t e m p l a t e u r l p a t t e r n s = p a t t e r n s ( ’ ’ , ( ’ ˆ a b o u t / $ ’ , d i r e c t t o t e m p l a t e , { ’ t e m p l a t e ’ : ’ a b o u t . h t m l ’ }) )Introduction Forms Authentication Generic Views Caching Others Admin
Introduction Forms Authentication Generic Views Caching Others Admin
Display objects and their lists
from d j a n g o . v i e w s . g e n e r i c . l i s t d e t a i l import \ o b j e c t d e t a i l , o b j e c t l i s t u r l ( ( r ’ ˆ o b j e c t s / p a g e ( ? P<page>[0−9]+)/ $ ’ , o b j e c t l i s t , d i c t ( i n f o d i c t ) ) , ( r ’ ˆ o b j e c t s / ( ? P<i d>[0−9]+)/ $ ’ , o b j e c t d e t a i l , d i c t ( i n f o d i c t ) ) , )
Introduction Forms Authentication Generic Views Caching Others Admin
List of generic views
d j a n g o . v i e w s . g e n e r i c . s i m p l e . d i r e c t t o t e m p l a t e d j a n g o . v i e w s . g e n e r i c . s i m p l e . r e d i r e c t t o d j a n g o . v i e w s . g e n e r i c . l i s t d e t a i l . o b j e c t l i s t d j a n g o . v i e w s . g e n e r i c . l i s t d e t a i l . o b j e c t d e t a i l d j a n g o . v i e w s . g e n e r i c . c r e a t e u p d a t e . c r e a t e o b j e c t d j a n g o . v i e w s . g e n e r i c . c r e a t e u p d a t e . u p d a t e o b j e c t d j a n g o . v i e w s . g e n e r i c . c r e a t e u p d a t e . d e l e t e o b j e c t
Introduction Forms Authentication Generic Views Caching Others Admin
Introduction Forms Authentication Generic Views Caching Others Admin
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 ;\
Introduction Forms Authentication Generic Views Caching Others Admin
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 d e f c a c h e f o r ( s e c o n d s , f e t c h = 0 ) : d e f c a c h e i t ( f u n c ) : d e f d e c o f u n c ( s e l f ) : v a l = c a c h e . g e t ( 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 : v a l = f u n c ( s e l f ) c a c h e . s e t ( s e l f . g e t c a c h e k e y ( f e t c h ) , v a l , s e c o n d s ) r e t u r n v a l r e t u r n d e c o f u n c r e t u r n c a c h e i t
Introduction Forms Authentication Generic Views Caching Others Admin
Apply on the blocks
c l a s s T w i t t e r B l o c k ( T w i t t e r S e a r c h B l o c k ) : t e m p l a t e n a m e = ’ c o n t e n t b l o c k a p p / t w i t t e r u s e r b l o c k . h t m l ’ a j a x t e m p l a t e = ” c o n t e n t b l o c k a p p / t w e e t s u s e r . h t m l ” @ c a c h e f o r ( 6 0∗6 0 , f e t c h =1) d e f f e t c h d a t a ( s e l f ) : import b e t t e r t w i t t e r tw = b e t t e r t w i t t e r . T w i t t e r ( e m a i l= ’ umoja com ’ , p a s s w o r d= ’ k o t o k o 1 9 1 1 ’ ) 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
Introduction Forms Authentication Generic Views Caching Others Admin
Different cache Backends
•
Database
•File System
•Local Memory
Introduction Forms Authentication Generic Views Caching Others Admin
Introduction Forms Authentication Generic Views Caching Others Admin
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 ’ , ’ d a t e t i m e ’ ) c l a s s CommentAdmin ( admin . ModelAdmin ) :
l i s t d i s p l a y = ( ’ t e x t ’ , )
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 )