a hands on guide to django

Post on 10-May-2015

3.570 views 0 download

Tags:

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 of a hands on guide to django

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

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

What is django

Django is a Web framework

Build on the python programming language

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

now to finish up

run syncdb

python manage.py syncdb

() Hands on to django November 15, 2009 35 / 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

that’s all folks

So Long And Thanks For All The Fish

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