introducing django

26
What is Django? Features References Introducing Django Horst Gutmann September 30, 2007 1 / 34

Upload: zerok

Post on 10-May-2015

4.336 views

Category:

Business


0 download

DESCRIPTION

A small presentation by me at the Barcamp Vienna in Sept. 2007 where I tried to give a short overview about some of the features I really love about Django :D

TRANSCRIPT

Page 1: Introducing Django

What is Django?Features

References

Introducing Django

Horst Gutmann

September 30, 2007

1 / 34

Page 2: Introducing Django

What is Django?Features

References

Disclaimer

I’m not of the developers of Django but merely a user who likeswhat he’s seen. My experience is limited to small sites that can becomfortably run on a shared host such as Dreamhost.And I want to apologize for the noise my laptop is probably goingto make during the presentation ;-)

2 / 34

Page 3: Introducing Django

What is Django?Features

References

GeneralWhat do you need?

What is Django?

Django is a high-level Python Web framework thatencourages rapid development and clean, pragmaticdesign.

[1]

Model-View-Controller for the Web

Written in Python

Explicit instead of implicit

Developed for the Lawrence Journal-World1 newspaper inKansas

1http://www2.ljworld.com/3 / 34

Page 4: Introducing Django

What is Django?Features

References

GeneralWhat do you need?

What is an MVC web framework?

Abstraction

5 / 34

Page 5: Introducing Django

What is Django?Features

References

GeneralWhat do you need?

Abstract HTTP

Do we really want to manually parse HTTP requests?

Frameworks should automate things like header creation

Allow easy cookie handling

6 / 34

Page 6: Introducing Django

What is Django?Features

References

GeneralWhat do you need?

Abstract database calls

Page . o b j e c t s . ge t ( i d =10)

instead of

SELECT ∗ FROM page WHERE i d =10;

Not to mention keeping it backend independent and mapping theresultset to objects in the used programming language.

8 / 34

Page 7: Introducing Django

What is Django?Features

References

GeneralWhat do you need?

Dispatch URLs

URLs or URL patterns should be mapped to functions orobjects

... that then create the output.

u r l p a t t e r n s += pa t t e r n s ( ’ ’ ,u r l ( ’ ˆ$ ’ , ’ v i ews . i nd ex ’ ) ,

)

10 / 34

Page 8: Introducing Django

What is Django?Features

References

GeneralWhat do you need?

MVC or MVT?

Django uses a little bit different naming for theModel-View-Controller pattern.

Models abstract the used data by defining classes for themand storing them in relational tables.

Views take the job of the controllers in MVC and basicallydefine, what the user gets to see. Functions, notclasses here.

Templates define how the users see the view

11 / 34

Page 9: Introducing Django

What is Django?Features

References

GeneralWhat do you need?

Projects and Applications

Project A project is for example your whole website. Hereyou store your central configuration and generaltemplates and images (just as an example).

Applications are where you store the actual functionality. Forexample a weblog would be an application. Anaccount system would be an application ...

Applications are a simple way to share common functionalitybetween various projects.

12 / 34

Page 10: Introducing Django

What is Django?Features

References

GeneralWhat do you need?

Easy start

Python2 (ideally 2.5 since sqlite is bundled with it)

a text editor

This is all you need to start developing with Django since Djangocomes with its own lightweight webserver to make it very easy foreveryone to start playing around with it.

2http://www.python.org13 / 34

Page 11: Introducing Django

What is Django?Features

References

GeneralWhat do you need?

Yet flexible

FastCGI

mod python

mod wsgi

SQLite

MySQL

PostgreSQL

Oracle in preparation

14 / 34

Page 12: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

Main features (for me)

A good collection of contributed applications

Administration interfaceAuthentication systemComments system...

Template language focused on inheritance

Simple form processing

No magic

15 / 34

Page 13: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

Administration interface I

16 / 34

Page 14: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

Administration interface II

Available in django.contrib.admin

Configured through model definitions

Configurable using templates (possible per model),JavaScripts and CSS

Currently a rewrite using newforms is in the pipe.

17 / 34

Page 15: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

Authentication system3

django.contrib.auth

Just hook it in, and you get a login screen

... and usergroups

... and custom permissions

3http://www.djangoproject.com/documentation/authentication/18 / 34

Page 16: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

django-registration5

By James Bennett4

Offers all you really need:

Simple registration formE-mail activation

Works with django.contrib.auth

4http://www.b-list.org/about/5http://code.google.com/p/django-registration/

19 / 34

Page 17: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

The Template Language

Very restrictive

... to keep application logic within the View and the ModelDon’t let templates change the state of your site!If you want to have complex code in your templates, you haveto define your own template tags in Python.

Inheritance between templates

You have to pass variables explicitly to the template from theview

20 / 34

Page 18: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

Template Basics

Variables Rendered with{{ variable_name }}

Tags allow you to do more sophisticated stuff whilekeeping the actual Python code out of the template:{% template_tag %}

Even conditional blocks are realized as tags.

Filters allow you to manipulate the output of a variable:{{ variable|filter }}

E.g.: Escaping strings, converting from Markdown toHTML, ...

22 / 34

Page 19: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

Template Inheritance6

The core of inheritance are blocks the templates.

base.html

<html><body><h1>{% block title %}{% endblock %}

</h1><div id="content">{% block content %}{% endblock %}

</div></body>

</html>

index.html

{% extends ’base.html’ %}{% block title %}

Index page{% endblock %}{% block content %}

...{% endblock %}

6http://www.djangoproject.com/documentation/templates/

#template-inheritance24 / 34

Page 20: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

newforms7

”newforms” because there was something before that ... let’signore the only form processing for now ;-)

Easy way to define forms

... to render them

... and to validate their data

Easily combinable with models (as you will see in thedemoapp)

7http://www.djangoproject.com/documentation/newforms/25 / 34

Page 21: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

Form definition

from django import newforms as formsc l a s s CommentForm( models . Form ) :

au tho r = form . Cha rF i e l d ( ’ Your name ’ )ema i l = form . Ema i l F i e l d ( ’ Your e−mai l )body = form . Cha rF i e l d ( ’Comment ’ ,

w idget=forms . Texta rea ( ) )

27 / 34

Page 22: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

Form output

{{ form . a s p }}

Results in:<p><l a b e l f o r=” i d a u t h o r ”>Your name :</ l a b e l>

<i npu t i d=” i d a u t h o r ” type=” t e x t ” name=” autho r ” maxlength=”100” /></p><p><l a b e l f o r=” i d ema i l ”>Your e−mai l :</ l a b e l>

<i npu t i d=” i d ema i l ” type=” t e x t ” name=” ema i l ” maxlength=”75” /></p><p><l a b e l f o r=” id body ”>Comment :</ l a b e l>

<t e x t a r ea i d=” id body ” rows=”10” c o l s=”40” name=”body”></ t e x t a r ea></p>

29 / 34

Page 23: Introducing Django

What is Django?Features

References

Contributed appsThe Template LanguageSimple Form Processing

Form data validation

Everything is done in Python, so it’s easily re-usable.

c l a s s CommentForm( forms . Form ) :

# . . .

def c l e a n ema i l ( s e l f ) :ema i l = s e l f . c l e a n e d da t a [ ” ema i l ” ] . s p l i t ( ”@” ) [ 1 ]i f ema i l i n [ ” ho tma i l . com” , ” yahoo . com” ] :

r a i s e Va l i d a t i o nE r r o r , ”Get a GMail account ! ”

31 / 34

Page 24: Introducing Django

What is Django?Features

References

“Django project site.” [Online]. Available:http://www.djangoproject.com/

32 / 34

Page 25: Introducing Django

What is Django?Features

References

Thank you

for your attention.Any last question?

33 / 34

Page 26: Introducing Django

What is Django?Features

References

Want to get some action?

If you liked what you saw, we could meet somewhere and build alittle website together :-)

34 / 34