What's Django?
quot;Django is a high-level Python Web framework
that encourages rapid development and clean,
pragmatic design.quot;
From Django official website
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Young But Strong
● Internal project of Lawrence Journal-World in
2003
● Should help journalists meet fast deadlines
● Should not stand in the journalists' way
● Got its name after the famous guitarrist Django
Reinhardt
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
MTV
Model-Template-View
● Model: What things are
● Templates: How things are presented
● Views: How things are processed
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Installation
Just get a tarball release or checkout the sources:
http://www.djangoproject.com/download/
Then:
# python setup.py install
... yeah, that it!
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Building The Database
$ ./manage.py syncdb
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Models
models.py, series of classes describing objects
Represent the database objects.
Never touch SQL again!
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Models
class Post(models.Model):
title = models.CharField(max_length = 500)
content = models.TextField()
date = models.DateTimeField(auto_now = True)
...
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Views
views.py, series of functions that will normally process some models and render HTML
Where the magic happen!
How to get all blog posts from the latest 5 days and order them by
descending date?
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Views
import datetime
def view_latest_posts(request):
# Last 5 days
date = datetime.datetime.now() -
datetime.timedelta(5)
posts = Post.objects.filter(date__gte =
date).order_by('-date')
return render_to_response('posts/show_posts.html',
{'posts': posts})
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Template
Will let you not repeat yourself!
Will save designers from the code.
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Template
{% extends quot;base.htmlquot; %}
{% block title %}Homepage{% endblock %}
{% block content %}
<h3>This will be some main content</h3>
{% for post in posts %}
<h4>{{ post.title }} on {{ post.date|date:quot;B d, Yquot;|upper }}<h4>
<p>{{ post.content }}</p>
{% endfor %}
{% url project.some_app.views.some_view some arguments %}
{% endblock %}
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
URLs
In Django, URLs are part of the
design!
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
URLs
urls.py use regular expressions to match URLs with views
urlpatterns = patterns('Project.some_app.views',
(r'^$', 'index'),
(r'^posts/(?P<r_id>d+)/$', 'view_latest_posts'),
(r'^create/$', 'create'),
)
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Forms
forms.py, series of classes that represent an HTML form
Will let you easily configure the expected type of the
inputs, error messages, labels, etc...
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Forms
class CreatePost(forms.Form):
title = forms.CharField(label = quot;Post Titlequot;,
max_length = 500,
widget = forms.TextInput(attrs={
'class': 'big_entry'
}))
content = forms.CharField()
tags = forms.CharField(required = False)
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Forms
def create_post(request):
if request.method == 'POST':
form = CreatePost(request.POST)
if form.is_valid():
# Create a new post object with data
# from form.cleaned_data
return HttpResponseRedirect('/index/')
else:
form = CreatePost()
return render_to_response('create.html', {
'form': form,
})
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Forms
The quick way...
<form action=quot;/create/quot; method=quot;POSTquot;>
{{ form.as_p }}
<input type=quot;submitquot; value=quot;Createquot; />
</form>
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com
Performance
For those who doubt...
http://www.alrond.com/en/2007/jan/25/performance-test-of-6-leading-
frameworks/
MSWL Caixanova · Vigo · May 22nd 2009 | Joaquim Rocha | jrocha@igalia.com