building pluggable web applications using django

Post on 10-May-2015

14.372 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Introduction Using django Features of django Reuseable apps End Notes

Building Pluggable Web Applications using

Lakshman Prasad

Agiliq Solutions

April 21, 2010

Introduction Using django Features of django Reuseable apps End Notes

• For building database driven web applications

• Emphasis on Reuse, DRY and simplicity

Introduction Using django Features of django Reuseable apps End Notes

• For building database driven web applications

• Emphasis on Reuse, DRY and simplicity

Introduction Using django Features of django Reuseable apps End Notes

Hypothetical Application

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 ’ ,

)

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 ’ ) ,

)

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>

. . .

Introduction Using django Features of django Reuseable apps End Notes

Thats it!

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

Introduction Using django Features of django Reuseable apps End Notes

Developed at Lawrence-Journal World

5 million hits per month

600 Contributors

Majority of the features community contributed

5 years since Open Source

Introduction Using django Features of django Reuseable apps End Notes

Philosophy

Automate repetitive tasks

Make Development Fast

Introduction Using django Features of django Reuseable apps End Notes

Convention Over Configuration

Follow Best Practices

Models-Templates-Views

Models

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 ( )

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 ( )

Introduction Using django Features of django Reuseable apps End Notes

Admin by models alone

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 )

List Page

Add an Entry

Auto Validation

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 .

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

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 ’ ) ,

)

Views

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 ! ’ )

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 ) )

Templates

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 %}

Features

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

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

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

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

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

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

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

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

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

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

Django Documentation

Introduction Using django Features of django Reuseable apps End Notes

General conventions adopted by the community

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 ] )

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 )

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

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

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

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

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

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

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 )

Introduction Using django Features of django Reuseable apps End Notes

Advanced

• Use Template Response

• Write Views as Classes

Introduction Using django Features of django Reuseable apps End Notes

Advanced

• Use Template Response

• Write Views as Classes

There is an app for that

For every size and style

Introduction Using django Features of django Reuseable apps End Notes

Github Search ”django”

Introduction Using django Features of django Reuseable apps End Notes

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

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

Django users

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

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

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

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.

Introduction Using django Features of django Reuseable apps End Notes

Enterprise Adoption Hurdles

• Multiple Databases

• Dynamic settings infrastructure

• Tools

Introduction Using django Features of django Reuseable apps End Notes

Enterprise Adoption Hurdles

• Multiple Databases

• Dynamic settings infrastructure

• Tools

Introduction Using django Features of django Reuseable apps End Notes

Enterprise Adoption Hurdles

• Multiple Databases

• Dynamic settings infrastructure

• Tools

Introduction Using django Features of django Reuseable apps End Notes

About Me• lakshman@agiliqsolutions.com, @becomingGuru• lakshmanprasad.com• Agiliq Solutions Formerly, Usware Technologies @agiliq• Team of Expert Django Developers, Happy Clients

http://www.agiliqsolutions.com/

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/

?

top related