4. 4
Django
A high-level Python web framework
Created in 2003, open sourced in 2005
Encourages rapid development and clean, pragmatic design
Widely supported, many deployment options
Focus on automation and DRY
“For perfectionists with deadlines”
MVC (MTV) Architecture
5. 5
Who uses Django?
BitBucket
DISQUS (serving 400 million people)
Pinterest
Instagram
dPaste
Mozilla (support.mozill, addons.mozilla)
NASA
PBS (Public Broadcasting Service)
The Washington Post, NY Times, LA Times, The Guardian
National Geographic, Discovery Channel
6. 6
Features
Object Relational Mapper - ORM
Template System
customizable Admin Interface, makes CRUD easy
Built-in light weight Web Server
URL design
Authentication / Authorization
Internationalization support
Cache framework, with multiple cache mechanisms
Free, and Great Documentation
9. 9
Virtualenv
pip “one project, one virtualenv”
projects with different dependencies, package versions
easier to deploy. Forget dependency hell!
10. 10
Django Architecture
Models Describes your data
Views Controls what users sees
Templates How user sees it
Controller URL dispatcher
DataBase
Model
View
Template URL dispatcher
Brower
11. 11
Project Creation
Creating new Project
django-admin.py startproject demoproject
A project is a collection of applications
Creating new Application
python manage.py startapp demosite
An application tries to provide a single, relatively self-contained set of related functions
Using the built-in web server
python manage.py runserver
Runs by default at port 8000
13. 13
Apps
use short, obvious, single-word names for your apps
many small apps is better than a few giant apps:
explain an app in a sentence. If you can't, split the app
rather than expand an app, write a new app
don't reinvent the wheel!
django.contrib
3rd-party apps
14. 14
Settings
use - multiple settings files: - per environment:
dev, testing, staging, production - per developer (local settings)
all settings files must inherit from base, so you can do:
INSTALLED_APPS += ('debug_toolbar', ) - version control all the settings!
15. 15
Models
you can use Django without a database
Or
describe your database layout in Python code with django ORM
16. 16
Models
from django.db import models
class Reporter(models.Model):
full_name = models.CharField(max_length=70)
def __str__(self): # __unicode__ on Python 2
return self.full_name
class Article(models.Model):
pub_date = models.DateField()
headline = models.CharField(max_length=200)
content = models.TextField() reporter = models.ForeignKey(Reporter)
def __str__(self): # __unicode__ on Python 2
return self.headline
18. 18
URLs
Map URLs in requests to code that can be executed
Regular expressions!
Subsections of your site can have their own urls.py modules
19. 19
URLs
Root URL should be configured in settings.py
o ROOT_URLCONF = 'app.urls'
Example
urlpatterns = patterns(' ',
(r'^articles-year/$', 'mysite.news.views.articles_year'),
)
20. 20
Views
Map Code that handles requests
Other frameworks often call these “controllers”
Basically a function that:
gets a request
returns text or a response
25. 25
Form
from django import forms
class MyForm(forms.Form):
name = forms.CharField(max_length=30)
email = forms.EmailField()
26. 26
Using a Form in a View
from myapp.forms import MyForm
def my_view(request):
form = MyForm(request.POST or None)
if request.method == "POST" and form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
# do something great with that data
return render(request, 'myapp/myform.html', { 'form': form })