SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
1.
Introduction to Django
Master in Free Software 2009/2010
Joaquim Rocha <jrocha@igalia.com>
3, July 2010
2.
What is it?
"Django is a highlevel Python Web framework
that encourages rapid development and
clean, pragmatic design."
From Django official website
Master in Free Software | 20092010 2
3.
What is it?
Internal project of Lawrence JournalWorld in
2003
Should help journalists meet fast deadlines
Should not stand in the journalists' way
Got its name after the famous guitarrist
Django Reinhardt
Master in Free Software | 20092010 3
4.
The framework
ObjectRelational Mapper
Automatic Admin Interface
Elegant URL Design
Powerful Template System
i18n
it's amazing...!
Master in Free Software | 20092010 4
5.
Big community
Django has a big community and an extensive list of
Django apps
Search for them in http://code.google.com
Other interesting websites:
* Django sites: http://www.djangosites.org
* Django people: http://www.djangopeople.com
Master in Free Software | 20092010 5
7.
DB Backend
Officially supported:
PostgreSQL
MySQL
SQLite
Oracle
Master in Free Software | 20092010 7
8.
Using Django
Master in Free Software | 20092010 8
9.
Installation
Just get a tarball release or checkout the sources:
http://djangoproject.com/download
Then:
# python setup.py install
Or:
# easy_install django
Master in Free Software | 20092010 9
10.
Development
Master in Free Software | 20092010 10
11.
Creating a project
$ djangoadmin.py startproject Project
Project
__init__.py
manage.py
settings.py
urls.py
Master in Free Software | 20092010 11
12.
Running the project
$ ./manage.py runserver
...and open your browse at localhost:8000
Master in Free Software | 20092010 12
13.
Running the project
Master in Free Software | 20092010 13
14.
Development
Django Projects have Apps
Apps are the projects' components
Master in Free Software | 20092010 14
15.
Creating an App
$ ./manage.py startapp my_app
my_app
__init__.py
models.py
tests.py
views.py
Master in Free Software | 20092010 15
16.
Building the database
$ ./manage.py syncdb
Master in Free Software | 20092010 16
17.
Project's configuration
Easy configuration in the settings.py file
Master in Free Software | 20092010 17
18.
Building the database
$ ./manage.py syncdb
Master in Free Software | 20092010 18
19.
Development
Django follows the MTV design pattern:
ModelTemplateView
Master in Free Software | 20092010 19
20.
Models
Models are a series of classes describing
objects
They represent the database objects
Never touch SQL again!
Master in Free Software | 20092010 20
21.
Models
class Post(models.Model):
title = models.CharField(max_length = 500)
content = models.TextField()
date = models.DateTimeField(auto_now = True)
...
Master in Free Software | 20092010 21
22.
Views
Views are a series of functions that normally
process some models and render HTML
It's where the magic happens!
How to get all blog posts from the latest 5 days and order them by
descending date?
Master in Free Software | 20092010 22
23.
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})
Master in Free Software | 20092010 23
24.
Templates
Will not let you repeat yourself!
Will save designers from the code.
Master in Free Software | 20092010 24
29.
Forms
Series of classes that represent an HTML form
Will let you easily configure the expected
type of the inputs, error messages, labels,
etc...
Master in Free Software | 20092010 29
30.
Forms
class CreatePost(forms.Form):
title = forms.CharField(label = "Post Title",
max_length = 500,
widget = forms.TextInput(attrs={
'class': 'big_entry'
}))
content = forms.CharField()
tags = forms.CharField(required = False)
Master in Free Software | 20092010 30
31.
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,
})
Master in Free Software | 20092010 31
32.
Forms
The quick way:
<form action="/create/"
method="POST">
{{ form.as_p }}
<input type="submit" value="Create"
/>
</form>
Master in Free Software | 20092010 32
33.
Where to go from here?
Master in Free Software | 20092010 33
34.
Django hosts
An extensive list is can be found at:
http://code.djangoproject.com/wiki/DjangoFriendlyWe
bHosts
Popular ones:
http://www.statopia.com/corporate/blog/2007/aug/05/
PopularDjangoHostingService/
Google's AppEngine is also Django friendly:
http://appengine.google.com/
Master in Free Software | 20092010 34
35.
Help
Django Docs
http://docs.djangoproject.com
Some books
● Learning Website Development with Django,
Packt
● Practical Django Projects, Apress
● Pro Django, Apress
Master in Free Software | 20092010 35
0 likes
Be the first to like this
Views
Total views
2,188
On SlideShare
0
From Embeds
0
Number of Embeds
2
You have now unlocked unlimited access to 20M+ documents!
Unlimited Reading
Learn faster and smarter from top experts
Unlimited Downloading
Download to take your learnings offline and on the go
You also get free access to Scribd!
Instant access to millions of ebooks, audiobooks, magazines, podcasts and more.
Read and listen offline with any device.
Free access to premium services like Tuneln, Mubi and more.