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
Falcon's Invoice Discounting: Your Path to Prosperity
Introducing Django
1. What is Django?
Features
References
Introducing Django
Horst Gutmann
September 30, 2007
1 / 34
2. What is Django?
Features
References
Disclaimer
I’m not of the developers of Django but merely a user who likes
what he’s seen. My experience is limited to small sites that can be
comfortably run on a shared host such as Dreamhost.
And I want to apologize for the noise my laptop is probably going
to make during the presentation ;-)
2 / 34
3. What is Django?
General
Features
What do you need?
References
What is Django?
Django is a high-level Python Web framework that
encourages rapid development and clean, pragmatic
design.
[1]
Model-View-Controller for the Web
Written in Python
Explicit instead of implicit
Developed for the Lawrence Journal-World1 newspaper in
Kansas
1
http://www2.ljworld.com/
3 / 34
4. What is Django?
General
Features
What do you need?
References
What is an MVC web framework?
Abstraction
5 / 34
5. What is Django?
General
Features
What do you need?
References
Abstract HTTP
Do we really want to manually parse HTTP requests?
Frameworks should automate things like header creation
Allow easy cookie handling
6 / 34
6. What is Django?
General
Features
What do you need?
References
Abstract database calls
Page . o b j e c t s . g e t ( i d =10)
instead of
SELECT ∗ FROM page WHERE i d =10;
Not to mention keeping it backend independent and mapping the
resultset to objects in the used programming language.
8 / 34
7. What is Django?
General
Features
What do you need?
References
Dispatch URLs
URLs or URL patterns should be mapped to functions or
objects
... that then create the output.
u r l p a t t e r n s += p a t t e r n s ( ’ ’ ,
u r l ( ’ ˆ$ ’ , ’ v i e w s . i n d e x ’ ) ,
)
10 / 34
8. What is Django?
General
Features
What do you need?
References
MVC or MVT?
Django uses a little bit different naming for the
Model-View-Controller pattern.
Models abstract the used data by defining classes for them
and storing them in relational tables.
Views take the job of the controllers in MVC and basically
define, what the user gets to see. Functions, not
classes here.
Templates define how the users see the view
11 / 34
9. What is Django?
General
Features
What do you need?
References
Projects and Applications
Project A project is for example your whole website. Here
you store your central configuration and general
templates and images (just as an example).
Applications are where you store the actual functionality. For
example a weblog would be an application. An
account system would be an application ...
Applications are a simple way to share common functionality
between various projects.
12 / 34
10. What is Django?
General
Features
What do you need?
References
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 Django
comes with its own lightweight webserver to make it very easy for
everyone to start playing around with it.
2
http://www.python.org
13 / 34
11. What is Django?
General
Features
What do you need?
References
Yet flexible
SQLite
FastCGI
MySQL
mod python
PostgreSQL
mod wsgi
Oracle in preparation
14 / 34
12. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
Main features (for me)
A good collection of contributed applications
Administration interface
Authentication system
Comments system
...
Template language focused on inheritance
Simple form processing
No magic
15 / 34
13. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
Administration interface I
16 / 34
14. What is Django? Contributed apps
Features The Template Language
References Simple 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
15. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
Authentication system3
django.contrib.auth
Just hook it in, and you get a login screen
... and usergroups
... and custom permissions
3
http://www.djangoproject.com/documentation/authentication/
18 / 34
16. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
django-registration5
By James Bennett4
Offers all you really need:
Simple registration form
E-mail activation
Works with django.contrib.auth
4
http://www.b-list.org/about/
5
http://code.google.com/p/django-registration/
19 / 34
17. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
The Template Language
Very restrictive
... to keep application logic within the View and the Model
Don’t let templates change the state of your site!
If you want to have complex code in your templates, you have
to define your own template tags in Python.
Inheritance between templates
You have to pass variables explicitly to the template from the
view
20 / 34
18. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
Template Basics
Variables Rendered with
{{ variable_name }}
Tags allow you to do more sophisticated stuff while
keeping 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 to
HTML, ...
22 / 34
19. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
Template Inheritance6
The core of inheritance are blocks the templates.
base.html index.html
<html> {% extends ’base.html’ %}
<body> {% block title %}
<h1> Index page
{% block title %} {% endblock %}
{% endblock %} {% block content %}
</h1> ...
<div id=quot;contentquot;> {% endblock %}
{% block content %}
{% endblock %}
</div>
</body>
</html>
6
http://www.djangoproject.com/documentation/templates/
#template-inheritance
24 / 34
20. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
newforms7
”newforms” because there was something before that ... let’s
ignore 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 the
demoapp)
7
http://www.djangoproject.com/documentation/newforms/
25 / 34
21. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
Form definition
from d j a n g o import newforms a s f o r m s
c l a s s CommentForm ( m o d e l s . Form ) :
a u t h o r = form . C h a r F i e l d ( ’ Your name ’ )
e m a i l = form . E m a i l F i e l d ( ’ Your e−m a i l )
body = form . C h a r F i e l d ( ’ Comment ’ ,
w i d g e t=f o r m s . T e x t a r e a ( ) )
27 / 34
22. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
Form output
{{ form . a s p }}
Results in:
<p> a b e l f o r=” i d a u t h o r ”>Your name :</ l a b e l>
<l
<i n p u t i d=” i d a u t h o r ” t y p e=” t e x t ” name=” a u t h o r ” m a x l e n g t h=” 100 ” /></p>
<p> a b e l f o r=” i d e m a i l ”>Your e−m a i l :</ l a b e l>
<l
<i n p u t i d=” i d e m a i l ” t y p e=” t e x t ” name=” e m a i l ” m a x l e n g t h=” 75 ” /> </p>
<p> a b e l f o r=” i d b o d y ”>Comment :</ l a b e l>
<l
<t e x t a r e a i d=” i d b o d y ” rows=” 10 ” c o l s=” 40 ” name=” body ”> t e x t a r e a>
</ </p>
29 / 34
23. What is Django? Contributed apps
Features The Template Language
References Simple Form Processing
Form data validation
Everything is done in Python, so it’s easily re-usable.
c l a s s CommentForm ( f o r m s . Form ) :
# ...
def c l e a n e m a i l ( s e l f ) :
e m a i l = s e l f . c l e a n e d d a t a [ ” e m a i l ” ] . s p l i t ( ”@” ) [ 1 ]
i f e m a i l i n [ ” h o t m a i l . com” , ” yahoo . com” ] :
r a i s e V a l i d a t i o n E r r o r , ” Get a GMail a c c o u n t ! ”
31 / 34
24. What is Django?
Features
References
“Django project site.” [Online]. Available:
http://www.djangoproject.com/
32 / 34
25. What is Django?
Features
References
Thank you
for your attention.
Any last question?
33 / 34
26. What is Django?
Features
References
Want to get some action?
If you liked what you saw, we could meet somewhere and build a
little website together :-)
34 / 34