a hands on guide to django

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

Upload: swee-meng-ng

Post on 10-May-2015

3.569 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: a hands on guide to django

Hands on to django

November 15, 2009

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

Page 2: a hands on guide to django

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

Page 3: a hands on guide to django

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

Page 4: a hands on guide to django

What is django

Django is a Web framework

Build on the python programming language

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

Page 5: a hands on guide to django

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

Page 6: a hands on guide to django

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

Page 7: a hands on guide to django

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.pyBefore that create a database in mysql

create database

create database exampledb;

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

Page 8: a hands on guide to django

A little setup Part 2

DATABASE ENGINE = ’ mysql ’DATABASE NAME = ’ exampledb ’DATABASE USER = ’ root ’DATABASE PASSWORD = ’ root ’

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

Page 9: a hands on guide to django

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

Page 10: a hands on guide to django

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 django . db i m p o r t models# C r e a t e your models h e r e .c l a s s Example ( models . Model ) :

name = models . 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

Page 11: a hands on guide to django

lets prepare our application

before we can run our application

a little setup

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

)

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

Page 12: a hands on guide to django

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

Page 13: a hands on guide to django

now to create a basic view

in exampleapps/views.py

from django . h t t p i m p o r t HttpResponsefrom django . 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 tfrom django . 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# C r e a t e your v i e w s h e r e .from models i m p o r t ∗d e f e x a m p l e v i e w ( r e q u e s t ) :

i f r e q u e s t . method == ’POST ’ :data = 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 . s a v e ( )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 ( ’ example / t e m p l a t e . html ’ ,

{ ’ r e s ’ : r e s })

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

Page 14: a hands on guide to django

add this extra views

d e f e x a m p l e l i k e ( r e q u e s t , i d ) :data = 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 + 1data . s a v 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 ( ’ / example / ’ )

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

Page 15: a hands on guide to django

add this extra views

d e f e x a m p l e h a t e ( r e q u e s t , i d ) :data = 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 − 1data . s a v 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 ( ’ / example / ’ )

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

Page 16: a hands on guide to django

now create template

<html><head></head><body><br/><a h r e f =’/ example / l o g o u t /’> l o g o u t </a><form method=’ post ’ a c t i o n =’/ example /’>F i l l i n your s t a t u s

<br/>< t e x t a r e a name=’ p o s t i n g ’></ t e x t a r e a ><br/>< i n p u t t y p e =’ submit ’ v a l u e =’ submit ’/></form><br/>{% f o r d i n r e s %}

{{d . name}} {{d . s c o r e }}<br/><a h r e f =”/example / l i k e /{{d . i d }}/”> l i k e </a> <ah r e f =”/example / hat e /{{d . i d }}/”>hate </a><br/>

{% e n d f o r %}</body></html>

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

Page 17: a hands on guide to django

almost there

first create urls.py in exampleapps

from django . 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 ∗u r l p a t t e r n s = p a t t e r n s ( ’ ’ ,

( r ’ ˆ $ ’ , e x a m p l e v i e w ) ,( r ’ ˆ l i k e /(\d+)/$ ’ , e x a m p l e l i k e ) ,( r ’ ˆ hat e /(\d+)/$ ’ , e x a m p l e h a t e ) ,)

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

Page 18: a hands on guide to django

still going

and add a new entry in the main urls.py

from django . c o n f . u r l s . d e f a u l t s i m p o r t ∗

# Uncomment t he n e x t two l i n e s to e n a b l e the admin :#from django . 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 ( )

u r l p a t t e r n s = p a t t e r n s ( ’ ’ ,( 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

Page 19: a hands on guide to django

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

Page 20: a hands on guide to django

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

Page 21: a hands on guide to django

first we import some modules

from django . c o n t r i b . auth . d e c o r a t o r s i m p o r t l o g i n r e q u i r e dfrom django . c o n t r i b . auth i m p o r t a u t h e n t i c a t efrom django . c o n t r i b . auth i m p o r t l o g i nfrom django . c o n t r i b . auth i m p o r t l o g o u tfrom django . c o n t r i b . auth . models i m p o r t User

BTW the apps is installed by default

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

Page 22: a hands on guide to django

lets enable registration.

The user registration just need the username,password,email

here is the view

d e f e x a m p l e r e g i s t e r ( r e q u e s t ) :i f r e q u e s t . method == ’POST ’ :

username = r e q u e s t .POST. g e t ( ’ username ’ )password = r e q u e s t .POST. g e t ( ’ password ’ )e m a i l = r e q u e s t .POST. g e t ( ’ emai l ’ )u s e r = User . o b j e c t s . c r e a t e u s e r ( username ,

emai l , password )u s e r . s a v 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 (

’/ example / l o g i n / ’ )e l s e :

r e t u r n r e n d e r t o r e s p o n s e (’ example / r e g i s t e r . html ’ , { } )

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

Page 23: a hands on guide to django

the templates

d e f e x a m p l e r e g i s t e r ( r e q u e s t ) :i f r e q u e s t . method == ’POST ’ :

username = r e q u e s t .POST. g e t ( ’ username ’ )password = r e q u e s t .POST. g e t ( ’ password ’ )e m a i l = r e q u e s t .POST. g e t ( ’ emai l ’ )u s e r = User . o b j e c t s . c r e a t e u s e r ( username

, e m a i l, password )

u s e r . s a v 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 (

’/ example / l o g i n / ’ )e l s e :

r e t u r n r e n d e r t o r e s p o n s e (’ example / r e g i s t e r . html ’ , { } )

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

Page 24: a hands on guide to django

lets login

d e f e x a m p l e l o g i n ( r e q u e s t ) :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 ( ’ password ’ )u s e r = a u t h e n t i c a t e ( username = username ,

password = p a s s w o r d )#c o n t i n u e d n e x t page

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

Page 25: a hands on guide to django

continued

#from p r e v i o u s pagei f u s e r i s not None :

i f u s e r . i s a c t i v e :l o g i n ( r e q u e s t , u s e r )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 (

’/ example / ’ )e l s 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 (’/ example / l o g i n / ’ )

e l s 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 (

’/ example / l o g i n / ’ )e l s e :

r e t u r n r e n d e r t o r e s p o n s e (’ example / l o g i n . html ’ , { } )

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

Page 26: a hands on guide to django

and the login page

<html><head></head><body><form a c t i o n =’/ example / l o g i n / ’ method=’ post ’><br/>< 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 ’/><br/>< l a b e l >password :</ l a b e l >< i n p u t t y p e =’ password ’ name=’ password ’/><br/>< 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

Page 27: a hands on guide to django

logout

d e f e x a m p l e l o g o u t ( r e q u e s t ) :l o g o u t ( 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 R e d i r e c t (

’/ example / l o g i n / ’ )

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

Page 28: a hands on guide to django

now to make it works

u r l p a t t e r n s = p a t t e r n s ( ’ ’ ,( r ’ ˆ $ ’ , e x a m p l e v i e w ) ,( 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 ’ ˆ hat e /(\d+)/$ ’ , e x a m p l e h a t e ) ,)

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

Page 29: a hands on guide to django

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

Page 30: a hands on guide to django

a little setup again

add the following line to settings.py

LOGIN URL = ’/ example / l o g i n / ’LOGOUT URL = ’/ example / l o g i n / ’

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

Page 31: a hands on guide to django

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

Page 32: a hands on guide to django

still going

uncomment admin stuff in in the main urls.py

from django . c o n f . u r l s . d e f a u l t s i m p o r t ∗

from django . c o n t r i b i m p o r t adminadmin . a u t o d i s c o v e r ( )

u r l p a t t e r n s = p a t t e r n s ( ’ ’ ,( 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

Page 33: a hands on guide to django

now to create admin.py

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

Page 34: a hands on guide to django

install the admin apps

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

)

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

Page 35: a hands on guide to django

now to finish up

run syncdb

python manage.py syncdb

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

Page 36: a hands on guide to django

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

Page 37: a hands on guide to django

that’s all folks

So Long And Thanks For All The Fish

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