Hands on to django




       November 15, 2009




()        Hands on to django   November 15, 2009   1 / 36
disclaimer




    this is based on my brief experience in django
    a lot is from my job experience
    a lot of the cod...
the slide and code is on the web




the code is bitbucket.org
http://bitbucket.org/sweemeng/barcamp-malacca/

slide is on...
What is django




   Django is a Web framework
   Build on the python programming language




        ()                ...
What you will need




You will need
    python, I use python 2.6, because ubuntu uses it
    a database, I use mysql
    ...
Lets start




To start a django project
The command on terminal
python django-admin startproject example

begin demo 1


...
A little setup Part 1


A few files is created form the command previously
File list
manage.py settings.py templates urls.p...
A little setup Part 2




DATABASE ENGINE = ’ mysql ’
DATABASE NAME = ’ exampledb ’
DATABASE USER = ’ r o o t ’
DATABASE P...
Let start an app


    A django project is consist of multiple applications
    To start an app, use manage.py
start a dja...
What ORM?


  django talk to database using ORM
  ORM == Object Relational Mapper
  It is used to model a database table i...
lets prepare our application


    before we can run our application
    a little setup


    INSTALLED APPS = (
        ’...
setup redux




    now setting up the database
creating the table
python manage.py syncdb

    when prompt to create new ...
now to create a basic view
in exampleapps/views.py
from 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
from d j...
add this extra views




def example like ( request , id ) :
    d a t a = Example . o b j e c t s . g e t ( i d=i d )
   ...
add this extra views




def example hate ( request , id ) :
    d a t a = Example . o b j e c t s . g e t ( i d=i d )
   ...
now create template

<html>
<head>
</head>
<body>
<b r/><a h r e f =’/ e x a m p l e / l o g o u t /’> l o g o u t </a>
<f...
almost there


first create urls.py in exampleapps


from 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...
still going

and add a new entry in the main urls.py


from 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 ...
now run the program




run the internal server
python manage.py runserver

and test the app by pointing your browser
poin...
Now to add some login




   before we continue
   django have many application that make live easier
   here we first look...
first we import some modules




from   django .   contrib   . auth . d e c o r a t o r s import l o g i n r e q u i
from  ...
lets enable registration.
   The user registration just need the username,password,email
   here is the view

   def examp...
the templates

def example register ( request ) :
    i f r e q u e s t . method == ’POST ’ :
          username = r e q u...
lets login




def example login ( request ) :
    i f r e q u e s t . method == ’POST ’ :
          u s e r n a m e = r e...
continued
        #from p r e v i o u s page
         i f u s e r i s n o t None :
               i f user . i s a c t i v...
and the login page

<html>
<head>
</head>
<body>
<form a c t i o n =’/ e x a m p l e / l o g i n / ’ method =’ p o s t ’>
...
logout




def example logout ( request ) :
    logout ( request )
    return HttpResponseRedirect (
             ’/ examp...
now to make it works




urlpatterns = patterns ( ’ ’ ,
             ( r ’ˆ $ ’ , example view ) ,
             ( r ’ ˆ l ...
to apply it to our apps




    to apply login needed for our views
    just add a line called @login required
    it will...
a little setup again




add the following line to settings.py


LOGIN URL = ’ / e x a m p l e / l o g i n / ’
LOGOUT URL ...
the best part of django




    admin page is free on django
    meaning you can just use enable it




         ()       ...
still going

uncomment admin stuff in in the main urls.py


from 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 ...
now to create admin.py




from d j a n g o . c o n t r i b i m p o r t admin
from m o d e l s i m p o r t ∗
admin . s i t...
install the admin apps



INSTALLED APPS = (
    ’ django . c o n t r i b   . auth ’ ,
    ’ django . c o n t r i b   . co...
now to finish up




run syncdb
python manage.py syncdb




         ()               Hands on to django   November 15, 200...
in the end




    this is just a demo on a few thing on django
    django offer a lot more
    builtin feed syndication,co...
that’s all folks




So Long And Thanks For All The Fish




         ()                 Hands on to django   November 15,...
Upcoming SlideShare
Loading in...5
×

a hands on guide to django

3,577

Published on

this is a presentation on django i do for barcamp Malacca

forgive me if it look hackish, i hack it in latex in a few hours

Published in: Technology
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,577
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
89
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

a hands on guide to django

  1. 1. Hands on to django November 15, 2009 () Hands on to django November 15, 2009 1 / 36
  2. 2. disclaimer this is based on my brief experience in django a lot is from my job experience a lot of the code does not look this way, i just clean it up and cram a lot of space () Hands on to django November 15, 2009 2 / 36
  3. 3. the slide and code is on the web the code is bitbucket.org http://bitbucket.org/sweemeng/barcamp-malacca/ slide is on slideshare.net http://www.slideshare.net/sweemenghacker/a-hands-on-guide-to-django () Hands on to django November 15, 2009 3 / 36
  4. 4. What is django Django is a Web framework Build on the python programming language () Hands on to django November 15, 2009 4 / 36
  5. 5. What you will need You will need python, I use python 2.6, because ubuntu uses it a database, I use mysql python-mysql for mysql and thats pretty much everything () Hands on to django November 15, 2009 5 / 36
  6. 6. Lets start To start a django project The command on terminal python django-admin startproject example begin demo 1 () Hands on to django November 15, 2009 6 / 36
  7. 7. A little setup Part 1 A few files is created form the command previously File list manage.py settings.py templates urls.py Now we concern on settings.py Before that create a database in mysql create database create database exampledb; () Hands on to django November 15, 2009 7 / 36
  8. 8. A little setup Part 2 DATABASE ENGINE = ’ mysql ’ DATABASE NAME = ’ exampledb ’ DATABASE USER = ’ r o o t ’ DATABASE PASSWORD = ’ r o o t ’ () Hands on to django November 15, 2009 8 / 36
  9. 9. Let start an app A django project is consist of multiple applications To start an app, use manage.py start a django apps python manage.py startapp exampleapp The newly create app folder should have example content init .py models.py tests.py urls.py views.py () Hands on to django November 15, 2009 9 / 36
  10. 10. What ORM? django talk to database using ORM ORM == Object Relational Mapper It is used to model a database table into object start create one in exampleapp/models.py from d j a n g o . db i m p o r t m o d e l s # Create your models here . c l a s s Example ( m o d e l s . Model ) : name = m o d e l s . T e x t F i e l d ( ) s c o r e = models . I n t e g e r F i e l d ( ) Django models field also have field like email, and image () Hands on to django November 15, 2009 10 / 36
  11. 11. lets prepare our application before we can run our application a little setup INSTALLED APPS = ( ’ django . c o n t r i b . auth ’ , ’ django . c o n t r i b . contenttypes ’ , ’ django . c o n t r i b . sessions ’ , ’ django . c o n t r i b . sites ’ , ’ exampleapp ’ , ) () Hands on to django November 15, 2009 11 / 36
  12. 12. setup redux now setting up the database creating the table python manage.py syncdb when prompt to create new superuser, just type yes and fill in the infor what ever you wanted () Hands on to django November 15, 2009 12 / 36
  13. 13. now to create a basic view in exampleapps/views.py from 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 from 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 R e d i r e c t from 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 # Create your views here . from m o d e l s i m p o r t ∗ def example view ( request ) : i f r e q u e s t . method == ’POST ’ : d a t a = Example ( name = r e q u e s t . POST . g e t ( ’ p o s t i n g ’ ) , s c o r e = 0) data . save () r e s = Example . o b j e c t s . a l l ( ) r e t u r n r e n d e r t o r e s p o n s e ( ’ e x a m p l e / t e m p l a t e . html ’ , { ’ res ’ : r e s }) () Hands on to django November 15, 2009 13 / 36
  14. 14. add this extra views def example like ( request , id ) : d a t a = Example . o b j e c t s . g e t ( i d=i d ) data . s c o r e = data . s c o r e + 1 data . save () r e t u r n H ttp Re sp ons eR ed ire ct ( ’/ example / ’) () Hands on to django November 15, 2009 14 / 36
  15. 15. add this extra views def example hate ( request , id ) : d a t a = Example . o b j e c t s . g e t ( i d=i d ) data . s c o r e = data . s c o r e − 1 data . save () r e t u r n H ttp Re sp ons eR ed ire ct ( ’/ example / ’) () Hands on to django November 15, 2009 15 / 36
  16. 16. now create template <html> <head> </head> <body> <b r/><a h r e f =’/ e x a m p l e / l o g o u t /’> l o g o u t </a> <form method =’ p o s t ’ a c t i o n =’/ e x a m p l e /’> F i l l in your s t a t u s <b r/>< t e x t a r e a name=’ p o s t i n g ’></ t e x t a r e a > <b r/><i n p u t t y p e =’ submit ’ v a l u e =’ submit ’/> </form> <b r/> {% f o r d i n r e s %} {{ d . name}} {{ d . s c o r e }}< b r/> <a h r e f =”/ e x a m p l e / l i k e /{{ d . i d }}/”> l i k e </a> <a h r e f =”/ e x a m p l e / h a t e /{{ d . i d }}/”> h at e </a><b r/> {% e n d f o r %} </body> () Hands on to django November 15, 2009 16 / 36
  17. 17. almost there first create urls.py in exampleapps from 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 ∗ from v i e w s i m p o r t ∗ urlpatterns = patterns ( ’ ’ , ( r ’ˆ $ ’ , example view ) , ( r ’ ˆ l i k e / ( d+)/$ ’ , e x a m p l e l i k e ) , ( r ’ ˆ h a t e / ( d+)/$ ’ , e x a m p l e h a t e ) , ) () Hands on to django November 15, 2009 17 / 36
  18. 18. still going and add a new entry in the main urls.py from 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 ∗ # Uncomment t h e n e x t two l i n e s t o e n a b l e t h e admin : #from d j a n g o . c o n t r i b i m p o r t admin #admin . a u t o d i s c o v e r ( ) urlpatterns = patterns ( ’ ’ , ( r ’ ˆ example / ’ , i n c l u d e ( ’ exampleapp . u r l s ’ ) ) , ( r ’ ˆ admin / ’ , i n c l u d e ( admin . s i t e . u r l s ) ) , ) () Hands on to django November 15, 2009 18 / 36
  19. 19. now run the program run the internal server python manage.py runserver and test the app by pointing your browser point browser to this address localhost:8000/examples/ () Hands on to django November 15, 2009 19 / 36
  20. 20. Now to add some login before we continue django have many application that make live easier here we first look at the authentication framework () Hands on to django November 15, 2009 20 / 36
  21. 21. first we import some modules from django . contrib . auth . d e c o r a t o r s import l o g i n r e q u i from django . contrib . auth import a u t h e n t i c a t e from django . contrib . auth import l o g i n from django . contrib . auth import l o g o u t from django . contrib . auth . models import User BTW the apps is installed by default () Hands on to django November 15, 2009 21 / 36
  22. 22. lets enable registration. The user registration just need the username,password,email here is the view def example register ( request ) : i f r e q u e s t . method == ’POST ’ : username = r e q u e s t . POST . g e t ( ’ username ’ ) p a s s w o r d = r e q u e s t . POST . g e t ( ’ p as sw or d ’ ) e m a i l = r e q u e s t . POST . g e t ( ’ e m a i l ’ ) u s e r = U s e r . o b j e c t s . c r e a t e u s e r ( username , email , password ) user . save () return HttpResponseRedirect ( ’/ example / l o g i n / ’) else : return render to response ( ’ e x a m p l e / r e g i s t e r . html ’ , { } ) () Hands on to django November 15, 2009 22 / 36
  23. 23. the templates def example register ( request ) : i f r e q u e s t . method == ’POST ’ : username = r e q u e s t . POST . g e t ( ’ username ’ ) p a s s w o r d = r e q u e s t . POST . g e t ( ’ p as sw or d ’ ) e m a i l = r e q u e s t . POST . g e t ( ’ e m a i l ’ ) u s e r = U s e r . o b j e c t s . c r e a t e u s e r ( username , email , password ) user . save () return HttpResponseRedirect ( ’/ example / l o g i n / ’) else : return render to response ( ’ e x a m p l e / r e g i s t e r . html ’ , { } ) () Hands on to django November 15, 2009 23 / 36
  24. 24. lets login def example login ( request ) : i f r e q u e s t . method == ’POST ’ : u s e r n a m e = r e q u e s t . POST . g e t ( ’ username ’ ) p a s s w o r d = r e q u e s t . POST . g e t ( ’ p as sw ord ’ ) u s e r = a u t h e n t i c a t e ( username = u s e r n a m e , password = password ) #c o n t i n u e d n e x t page () Hands on to django November 15, 2009 24 / 36
  25. 25. continued #from p r e v i o u s page i f u s e r i s n o t None : i f user . i s a c t i v e : login ( request , user ) return HttpResponseRedirect ( ’/ example / ’) else : return HttpResponseRedirect ( ’/ example / l o g i n / ’) else : return HttpResponseRedirect ( ’/ example / l o g i n / ’) else : return render to response ( ’ e x a m p l e / l o g i n . html ’ , { } ) () Hands on to django November 15, 2009 25 / 36
  26. 26. and the login page <html> <head> </head> <body> <form a c t i o n =’/ e x a m p l e / l o g i n / ’ method =’ p o s t ’> <b r/>< l a b e l >username :</ l a b e l > <i n p u t t y p e =’ t e x t ’ name=’ username ’/> <b r/>< l a b e l >p a s s w o r d :</ l a b e l > <i n p u t t y p e =’ passwo rd ’ name=’ pa ss wo rd ’/> <b r/><i n p u t t y p e =’ submit ’ v a l u e =’ submit ’/> </form> </body> </html> () Hands on to django November 15, 2009 26 / 36
  27. 27. logout def example logout ( request ) : logout ( request ) return HttpResponseRedirect ( ’/ example / l o g i n / ’) () Hands on to django November 15, 2009 27 / 36
  28. 28. now to make it works urlpatterns = patterns ( ’ ’ , ( r ’ˆ $ ’ , example view ) , ( r ’ ˆ l o g i n /$ ’ , e x a m p l e l o g i n ) , ( r ’ ˆ r e g i s t e r /$ ’ , e x a m p l e r e g i s t e r ) , ( r ’ ˆ l o g o u t /$ ’ , e x a m p l e l o g o u t ) , ( r ’ ˆ l i k e / ( d+)/$ ’ , e x a m p l e l i k e ) , ( r ’ ˆ h a t e / ( d+)/$ ’ , e x a m p l e h a t e ) , ) () Hands on to django November 15, 2009 28 / 36
  29. 29. to apply it to our apps to apply login needed for our views just add a line called @login required it will force a login () Hands on to django November 15, 2009 29 / 36
  30. 30. a little setup again add the following line to settings.py LOGIN URL = ’ / e x a m p l e / l o g i n / ’ LOGOUT URL = ’ / e x a m p l e / l o g i n / ’ () Hands on to django November 15, 2009 30 / 36
  31. 31. the best part of django admin page is free on django meaning you can just use enable it () Hands on to django November 15, 2009 31 / 36
  32. 32. still going uncomment admin stuff in in the main urls.py from 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 ∗ from d j a n g o . c o n t r i b i m p o r t admin admin . a u t o d i s c o v e r ( ) urlpatterns = patterns ( ’ ’ , ( r ’ ˆ example / ’ , i n c l u d e ( ’ exampleapp . u r l s ’ ) ) , ( r ’ ˆ admin / ’ , i n c l u d e ( admin . s i t e . u r l s ) ) , ) () Hands on to django November 15, 2009 32 / 36
  33. 33. now to create admin.py from d j a n g o . c o n t r i b i m p o r t admin from m o d e l s i m p o r t ∗ admin . s i t e . r e g i s t e r ( Example ) () Hands on to django November 15, 2009 33 / 36
  34. 34. install the admin apps INSTALLED APPS = ( ’ django . c o n t r i b . auth ’ , ’ django . c o n t r i b . contenttypes ’ , ’ django . c o n t r i b . sessions ’ , ’ django . c o n t r i b . sites ’ , ’ django . c o n t r i b . admin ’ , ’ exampleapp ’ , ) () Hands on to django November 15, 2009 34 / 36
  35. 35. now to finish up run syncdb python manage.py syncdb () Hands on to django November 15, 2009 35 / 36
  36. 36. in the end this is just a demo on a few thing on django django offer a lot more builtin feed syndication,commenting,tagging,GIS () Hands on to django November 15, 2009 36 / 36
  37. 37. that’s all folks So Long And Thanks For All The Fish () Hands on to django November 15, 2009 37 / 36
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×