building pluggable web applications using django

78
Introduction Using django Features of django Reuseable apps End Notes Building Pluggable Web Applications using Lakshman Prasad Agiliq Solutions April 21, 2010

Upload: lakshman-prasad

Post on 10-May-2015

14.376 views

Category:

Technology


3 download

DESCRIPTION

A talk introducing django and using it effectively, at Great Indian Developer Summit.

TRANSCRIPT

Page 1: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Building Pluggable Web Applications using

Lakshman Prasad

Agiliq Solutions

April 21, 2010

Page 2: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• For building database driven web applications

• Emphasis on Reuse, DRY and simplicity

Page 3: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• For building database driven web applications

• Emphasis on Reuse, DRY and simplicity

Page 4: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Hypothetical Application

Page 5: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Install a pluggable app django-star-rating

$pip install django-star-rating

INSTALLED APPS = (’ d jango . c o n t r i b . auth ’ ,’ d jango . c o n t r i b . c o n t e n t t y p e s ’ ,’ d jango . c o n t r i b . s e s s i o n s ’ ,’ d jango . c o n t r i b . s i t e s ’ ,’ d jango . c o n t r i b . admin ’ ,’ s t a r r a t i n g ’ ,

)

Page 6: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Point to an url pattern

from b l o g . models import Post

u r l p a t t e r n s = p a t t e r n s ( ’ ’ ,( r ’ ˆ$ ’ , ’ b l o g . v i e w s . i n d e x ’ ) ,( r ’ s t a r i n g /$ ’ ,{model : Post } ,’ s t a r r a t i n g . v i e w s . s t a r ’ ) ,

)

Page 7: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Put the rating where you want!

<html><head>

< t i t l e>{{ page . t i t l e }}</ t i t l e><body>

{% s t a r r a t i n g %}<div>

{{ page . c o n t e n t }}</ div>

. . .</body>

. . .

Page 8: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Thats it!

Page 9: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

IntroductionDefinitionA Case Study: Youtube StarsOverviewPhilosophy

Using djangoMTV

Features of djangoFeatures

Reuseable appsWriting reuseable appsCommunity Applications

End NotesDjango StatsCommon Enterprise HurdlesOther

Page 10: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Developed at Lawrence-Journal World

Page 11: Building Pluggable Web Applications using Django

5 million hits per month

Page 12: Building Pluggable Web Applications using Django

600 Contributors

Page 13: Building Pluggable Web Applications using Django

Majority of the features community contributed

Page 14: Building Pluggable Web Applications using Django

5 years since Open Source

Page 15: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Philosophy

Page 16: Building Pluggable Web Applications using Django

Automate repetitive tasks

Page 17: Building Pluggable Web Applications using Django

Make Development Fast

Page 18: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Convention Over Configuration

Page 19: Building Pluggable Web Applications using Django

Follow Best Practices

Page 20: Building Pluggable Web Applications using Django

Models-Templates-Views

Page 21: Building Pluggable Web Applications using Django

Models

Page 22: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Model Syntax

from django . db import models

c l a s s Post ( models . Model ) :t i t l e = models . C h a r F i e l d ( m a x l e n g t h =100)t e x t = models . T e x t F i e l d ( )d a t e t i m e = models . DateTimeFie ld ( )

c l a s s Meta :o r d e r i n g = ( ’−d a t e t i m e ’ , )

def u n i c o d e ( s e l f ) :return s e l f . t i t l e

c l a s s Comment ( models . Model ) :p o s t = models . Fore ignKey ( Post )t e x t = models . T e x t F i e l d ( )

Page 23: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Model API

>>>from b l o g . models import Post , Comment>>>p o s t = Post . o b j e c t s . a l l ( ) [ 0 ]>>>post comments = p o s t . comment set . a l l ( )

Page 24: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Admin by models alone

Page 25: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Admin Syntax

from django . c o n t r i b import adminfrom models 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 )

Page 26: Building Pluggable Web Applications using Django

List Page

Page 27: Building Pluggable Web Applications using Django

Add an Entry

Page 28: Building Pluggable Web Applications using Django

Auto Validation

Page 29: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Ugly urls

h t t p : / / a r t . com/ a r t g a l l e r y / d e f a u l t . asp ?s i d =9DF4BC0580DF11D3ACB60090271E26A8

&command= f r e e l i s t

h t t p : / / p r e v i e w . ynot . com/ c g i b i n /nd CGI 50 . c g i / YnotPhoenix /CFsMain .

Page 30: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Good urls

h t t p : / /www. w i r e d . com/ a p p l e /MacbookPro/

h t t p : / / devmarch . com/ d e v e l o p e r s u m m i t / s p e a k e r s . html

Page 31: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

django url pattern

from b l o g . models import Post

u r l p a t t e r n s = p a t t e r n s ( ’ ’ ,( r ’ ˆ$ ’ , ’ b l o g . v i e w s . i n d e x ’ ) ,( r ’ s t a r i n g /$ ’ ,{model : Post } ,’ s t a r r a t i n g . v i e w s . s t a r ’ ) ,

)

Page 32: Building Pluggable Web Applications using Django

Views

Page 33: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

django views

from django . h t t p import HttpResponse

def p o s t ( r e q u e s t ) :return HttpResponse ( ’ H e l l o World ! ’ )

Page 34: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

django views

def p o s t ( r e q u e s t , p o s t i d ) :p o s t = Post . o b j e c t s . g e t ( pk=p o s t i d )i f r e q . method == ’POST ’ :

comment form = CommentForm ( r e q u e s t .POST)comment = comment form . s a v e ( )

p a y l o a d = { ’ p o s t ’ : post ,’ comments ’ :Comment . o b j e c t s . f i l t e r ( p o s t i d=p o s t i d ) ,’ comment form ’ : CommentForm ( )}

return r e n d e r t o r e s p o n s e ( ’ p o s t . html ’ ,pay load ,R e q u e s t C o n t e x t ( r e q ) )

Page 35: Building Pluggable Web Applications using Django

Templates

Page 36: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

django template

{% e x t e n d s ” base . html ” %}{% b l o c k body %}

{% i f u s e r . i s a u t h e n t i c a t e d %}Welcome {{ u s e r . g e t f u l l n a m e }}

{% e l s e %}{% i n c l u d e ” l o g i n . html ” %}

{% e n d i f %}

. . .

{% e n d b l o c k %}

Page 37: Building Pluggable Web Applications using Django

Features

Page 38: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 39: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 40: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 41: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 42: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 43: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 44: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 45: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 46: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 47: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

• Admin Interface

• Generic Views

• Testing Tools

• Sessions

• Authentication

• Caching

• Internationalization

• RSS

• CSRF protection

• File Storage

Page 48: Building Pluggable Web Applications using Django

Django Documentation

Page 49: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

General conventions adopted by the community

Page 50: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Use template tags

<p>The t ime i s {% c u r r e n t t i m e ”%I :%M %p” %}.</p>

from django import t e m p l a t edef d o c u r r e n t t i m e ( p a r s e r , token ) :

tag name , f o r m a t s t r i n g = token . s p l i t c o n t e n t s ( )return CurrentTimeNode ( f o r m a t s t r i n g [ 1 : −1 ] )

Page 51: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Use Signals

from django . c o r e . s i g n a l s import r e q u e s t f i n i s h e d

r e q u e s t f i n i s h e d . c o n n e c t ( m y c a l l b a c k )

from django . db . models . s i g n a l s import p r e s a v efrom myapp . models import MyModel

def m y h a n d l e r ( sender , ∗∗ kwargs ) :. . .

p r e s a v e . c o n n e c t ( my handler , s e n d e r=MyModel )

Page 52: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Basics

• Take template_name and extra_context every where

• Write urls in applications

• Import from the application level

• Prefix template name with directory

• Use MEDIA_URL in templates

• Reverse url patterns

Page 53: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Basics

• Take template_name and extra_context every where

• Write urls in applications

• Import from the application level

• Prefix template name with directory

• Use MEDIA_URL in templates

• Reverse url patterns

Page 54: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Basics

• Take template_name and extra_context every where

• Write urls in applications

• Import from the application level

• Prefix template name with directory

• Use MEDIA_URL in templates

• Reverse url patterns

Page 55: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Basics

• Take template_name and extra_context every where

• Write urls in applications

• Import from the application level

• Prefix template name with directory

• Use MEDIA_URL in templates

• Reverse url patterns

Page 56: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Basics

• Take template_name and extra_context every where

• Write urls in applications

• Import from the application level

• Prefix template name with directory

• Use MEDIA_URL in templates

• Reverse url patterns

Page 57: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Basics

• Take template_name and extra_context every where

• Write urls in applications

• Import from the application level

• Prefix template name with directory

• Use MEDIA_URL in templates

• Reverse url patterns

Page 58: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Basics Done right

def r e g i s t e r ( r e q u e s t , backend , s u c c e s s u r l=None ,f o r m c l a s s=None ,d i s a l l o w e d u r l=’ r e g i s t r a t i o n d i s a l l o w e d ’ ,template name=’ r e g i s t r a t i o n / r e g i s t r a t i o n f o r m . html ’ ,e x t r a c o n t e x t=None ) :

. . .

return r e n d e r t o r e s p o n s e ( template name ,{ ’ form ’ : form } ,c o n t e x t )

Page 59: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Advanced

• Use Template Response

• Write Views as Classes

Page 60: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Advanced

• Use Template Response

• Write Views as Classes

Page 61: Building Pluggable Web Applications using Django

There is an app for that

Page 62: Building Pluggable Web Applications using Django

For every size and style

Page 63: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Github Search ”django”

Page 64: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Page 65: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Pinax Features• openid support• email verification• password management• site announcements• a notification framework• user-to-user messaging• friend invitation (both internal and external to the site)• a basic twitter clone• oembed support• gravatar support• interest groups (called tribes)• projects with basic task and issue management• threaded discussions• wikis with multiple markup support• blogging• bookmarks• tagging• contact import (from vCard, Google or Yahoo)• photo management

Page 66: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

django-mingus

d j a n g o e x t e n s i o n sb a s i cf l a t b l o c k sd i s q u snavbard j a n g o d b l o gs o r loembdedt e m p l a t e u t i l sd j a n g o p r o x yc o m p r e s s o rd jango markupg o o g l e a n a l y t i c s

v i e w c a c h e u t i l sc o n t a c t f o r mhoneypots u g a rquotemedjango− s t a t i c f i l e sdjango−b i t l ydjango−t w i t t e rpython−t w i t t e rdjango−wysiwygdjango−s l i m m e rdjango−c r o p p e rdjango−r e q u e s t

Page 67: Building Pluggable Web Applications using Django

Django users

Page 68: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Popular Users

• Media• LA Times• NY Times• Washington Post• Guardian

• Web2.0• Mahalo: 10 million Page views• Pownce, SixApart

• Full List: djangosites.com

Page 69: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Popular Users

• Media• LA Times• NY Times• Washington Post• Guardian

• Web2.0• Mahalo: 10 million Page views• Pownce, SixApart

• Full List: djangosites.com

Page 70: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Popular Users

• Media• LA Times• NY Times• Washington Post• Guardian

• Web2.0• Mahalo: 10 million Page views• Pownce, SixApart

• Full List: djangosites.com

Page 71: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

NASA

After an extensive trade study, we selected Django ...as the first and primary application environment for theNebula Cloud.

Page 72: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Enterprise Adoption Hurdles

• Multiple Databases

• Dynamic settings infrastructure

• Tools

Page 73: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Enterprise Adoption Hurdles

• Multiple Databases

• Dynamic settings infrastructure

• Tools

Page 74: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Enterprise Adoption Hurdles

• Multiple Databases

• Dynamic settings infrastructure

• Tools

Page 75: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

About Me• [email protected], @becomingGuru• lakshmanprasad.com• Agiliq Solutions Formerly, Usware Technologies @agiliq• Team of Expert Django Developers, Happy Clients

Page 76: Building Pluggable Web Applications using Django

http://www.agiliqsolutions.com/

Page 77: Building Pluggable Web Applications using Django

Introduction Using django Features of django Reuseable apps End Notes

Image Attributions

ht tp : //www. f l i c k r . com/ photos / t e j e d o r o d e l u z /3157690060/ht tp : //www. f l i c k r . com/ photos /23820645@N05/4287681570/ht tp : //www. f l i c k r . com/ photos / a i d a n j o n e s /3575000735/ht tp : // j a c o b i a n . org /h t tp : // s a n j u a n c o l l e g e . edu/ l i b / images / p h i l o s o p h y b r a i n . j pgh t tp : //www. f l i c k r . com/ photos /uhop /105062059/ht tp : // s3 . amazonaws . com/memebox/ up load s /136/ e x p o n e n t i a l g r a p h 2 . j pgh t tp : // geekandpoke . typepad . com/geekandpoke / images /2008/06/03/ s e xp l 1 8 . j pgh t tp : //www. f l i c k r . com/ photos /go /253819/ht tp : // a round the sphe r e . f i l e s . wo rdp re s s . com/2009/05/ sw i s s−army−k n i f e . j pgh t tp : //www. f r e e f o t o . com/ images /41/04/41 04 9−−−Keep−Le f t web . j pgh t tp : //www. f l i c k r . com/ photos / o r i n r o b e r t j o h n /114430223/

Page 78: Building Pluggable Web Applications using Django

?