django introduction
TRANSCRIPT
![Page 1: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/1.jpg)
Introduction to Django
Master in Free Software 2009/2010
Joaquim Rocha <[email protected]>
3, July 2010
![Page 2: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/2.jpg)
Master in Free Software | 20092010 2
What is it?
"Django is a highlevel Python Web framework that encourages rapid development and clean, pragmatic design."
From Django official website
![Page 3: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/3.jpg)
Master in Free Software | 20092010 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
![Page 4: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/4.jpg)
Master in Free Software | 20092010 4
The framework
ObjectRelational Mapper
Automatic Admin Interface
Elegant URL Design
Powerful Template System
i18n
it's amazing...!
![Page 5: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/5.jpg)
Master in Free Software | 20092010 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
![Page 6: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/6.jpg)
Master in Free Software | 20092010 6
Deployment
FastCGI
mod_python
mod_wsgi
...
![Page 7: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/7.jpg)
Master in Free Software | 20092010 7
DB Backend
Officially supported:
PostgreSQLMySQLSQLiteOracle
![Page 8: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/8.jpg)
Master in Free Software | 20092010 8
Using Django
![Page 9: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/9.jpg)
Master in Free Software | 20092010 9
Installation
Just get a tarball release or checkout the sources:
http://djangoproject.com/download
Then:
# python setup.py install
Or:
# easy_install django
![Page 10: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/10.jpg)
Master in Free Software | 20092010 10
Development
![Page 11: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/11.jpg)
Master in Free Software | 20092010 11
Creating a project
$ djangoadmin.py startproject Project
Project
__init__.py
manage.py
settings.py
urls.py
![Page 12: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/12.jpg)
Master in Free Software | 20092010 12
Running the project
$ ./manage.py runserver
...and open your browse at localhost:8000
![Page 13: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/13.jpg)
Master in Free Software | 20092010 13
Running the project
![Page 14: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/14.jpg)
Master in Free Software | 20092010 14
Development
Django Projects have Apps
Apps are the projects' components
![Page 15: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/15.jpg)
Master in Free Software | 20092010 15
Creating an App
$ ./manage.py startapp my_app
my_app
__init__.py
models.py
tests.py
views.py
![Page 16: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/16.jpg)
Master in Free Software | 20092010 16
Building the database
$ ./manage.py syncdb
![Page 17: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/17.jpg)
Master in Free Software | 20092010 17
Project's configuration
Easy configuration in the settings.py file
![Page 18: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/18.jpg)
Master in Free Software | 20092010 18
Building the database
$ ./manage.py syncdb
![Page 19: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/19.jpg)
Master in Free Software | 20092010 19
Development
Django follows the MTV design pattern:
ModelTemplateView
![Page 20: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/20.jpg)
Master in Free Software | 20092010 20
Models
Models are a series of classes describing objects
They represent the database objects
Never touch SQL again!
![Page 21: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/21.jpg)
Master in Free Software | 20092010 21
Models
class Post(models.Model):
title = models.CharField(max_length = 500) content = models.TextField() date = models.DateTimeField(auto_now = True) ...
![Page 22: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/22.jpg)
Master in Free Software | 20092010 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?
![Page 23: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/23.jpg)
Master in Free Software | 20092010 23
Viewsimport 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})
![Page 24: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/24.jpg)
Master in Free Software | 20092010 24
Templates
Will not let you repeat yourself!
Will save designers from the code.
![Page 25: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/25.jpg)
Master in Free Software | 20092010 25
Templates
<html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> {% block content %}{% endblock %} </body></html>
![Page 26: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/26.jpg)
Master in Free Software | 20092010 26
Templates{% extends "base.html" %}
{% 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:"B d, Y"|upper }}<h4>
<p>{{ post.content }}</p> {% endfor %}
{% url project.some_app.views.some_view some arguments %}
{% endblock %}
![Page 27: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/27.jpg)
Master in Free Software | 20092010 27
URLs
In Django, the URLs are part of the design!
urls.py use regular expressions to match URLs with Views
![Page 28: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/28.jpg)
Master in Free Software | 20092010 28
URLs
urlpatterns = patterns('Project.some_app.views', (r'^$', 'index'),
(r'^posts/(?P<r_id>\d+)/$', 'view_latest_posts'),
(r'^create/$', 'create'),
url(r'^view/post/(?P<p_id>\d+)/$', 'view', name = 'view_post'),)
![Page 29: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/29.jpg)
Master in Free Software | 20092010 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...
![Page 30: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/30.jpg)
Master in Free Software | 20092010 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)
![Page 31: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/31.jpg)
Master in Free Software | 20092010 31
Formsdef 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, })
![Page 32: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/32.jpg)
Master in Free Software | 20092010 32
Forms
The quick way:
<form action="/create/" method="POST"> {{ form.as_p }} <input type="submit" value="Create" /></form>
![Page 33: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/33.jpg)
Master in Free Software | 20092010 33
Where to go from here?
![Page 34: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/34.jpg)
Master in Free Software | 20092010 34
Django hosts
An extensive list is can be found at:
http://code.djangoproject.com/wiki/DjangoFriendlyWebHosts
Popular ones:
http://www.statopia.com/corporate/blog/2007/aug/05/PopularDjangoHostingService/
Google's AppEngine is also Django friendly:
http://appengine.google.com/
![Page 35: Django introduction](https://reader034.vdocuments.site/reader034/viewer/2022052315/554e729ab4c90545698b4ade/html5/thumbnails/35.jpg)
Master in Free Software | 20092010 35
Help
Django Docs
http://docs.djangoproject.com
Some books● Learning Website Development with Django,
Packt● Practical Django Projects, Apress● Pro Django, Apress