3. Introduction
3
Django is a high-level Python Web framework that encourages rapid
development and clean, pragmatic design.(快速,实用开发.)
The Web framework for perfectionists with deadlines.(期限终结者)
Django makes it easier to build better Web apps more quickly and
with less code.(用少量地代码快速创建)
Encourages rapid development and clean, pragmatic design.
Named after famous Guitarist Django Reinhardt
Developed by Adrian Holovaty & Jacob Kaplan-moss
Created in 2003, open sourced in 2005
1.0 Version released in Sep 3 2008, now 1.7.1
6. 6
Django Architecture(MTVC)
Models Describes your data
Views Controls what users sees
Templates How user sees it
Controller URL dispatcher
1.URL调度中心(urls.py)通过匹配相应的URL,将请求移交并调用
相应的视图函数.若相应版本的缓存页存在则直接返回.django
不仅提供页面级别的缓存功能,也提供更加细粒度的缓存.
2.视图函数(views.py)一般会通过读写数据库,或者其他任务,响
应请求.
3.模型(models.py)定义了python中的数据结构及相关数据库交
互操作.除了关系型数据库(Mysql, PostgreSQL,SQLite等)之外,
其他的存储机制如XML,文本文件,LDAP等非关系型数据库也是支
持的.
4.执行完相应的操作后,视图函数一般会返回HTTP响应对象(数据
一般通过模板输出)给浏览器.在返回之前,视图函数也可以选择
把响应对象存储在缓存中.
5.模板系统返回HTML页面.Django模板提供易于上手的语法,足以
实现表示逻辑的所有需求.
7. 7
Django Architecture(MTVC)
M
Model
V
View
C
Controll
er
M
Model
T
Templ
ate
V
View
C
Contro
ller
Django
Framework
9. 9
Django Middleware
During the request phase :
process_request(request)
process_view(request, view_func,
view_args, view_kargs)
During the response phase:
process_exception(request, exception)
(only if the view raised an exception)
process_template_response(request,
response)
(only for template responses)
process_response(request, response)
10. 10
Installation
Prerequisites
Python
PIP for installing Python packages (http://www.pip-installer.org/en/latest/installing.html)
pip install Django==1.6.5
o OR https://www.djangoproject.com/download/ - python setup.py install
pip install mysql-python
o MySQL on windows https://pypi.python.org/pypi/MySQL-python/1.2.4
Add Python and Django to env path
o PYTHONPATH D:Python27
o Path D:Python27; D:Python27Libsite-packages; D:Python27Libsite-packagesdjangobin;
Testing installation
o shell> import django; django.VERSION;
11. 11
Project / Site 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
python manage.py runserver 80
Runs by default at port 8000
It checks for any error and validate the models. Throws errors/warnings for any misconfigurations and invalid
entries.
15. 15
Settings
Project settings.py overrides from <python>/Lib/site-packgaes/django/conf/global_settings.py
Set DJANGO_SETTINGS_MODULE for your Project, tells django which settings to be used. (demoproject.settings)
export/set DJANGO_SETTINGS_MODULE=demoproject.settings
For server mod_wsgi: os.environ['DJANGO_SETTINGS_MODULE'] = 'demoproject.settings'
DEBUG True or False
DATABASES: {ENGINE, NAME) 'mysql', 'sqlite3' , 'oracle‘ or ‘mongodb’.. etc
ROOT_URLCONF
INSTALLED_APPS To any app you want to add into your project. Name, Lable must be unique.
MIDDLEWARE_CLASSES A tuple of middleware classes which process request, response, exceptions to use.
STATIC_ROOT To any server static files. (can add more dirs to STATICFILES_DIRS) STATICFILES_FINDERS.
STATIC_URL To serve static files.
TEMPLATE_DIRS Template directories
Using settings in Python code
from django.conf import settings
if settings.DEBUG:
# Do something
16. 16
Project Directory Structure
demosite/ ---------------------------------- Just a container for your project. Its name doesn’t matter to Django;
manage.py ------------------------- A command-line utility that lets you interact with this Django project in various ways.
demosite/ ------------------------- Actual Python package for your project. Use this name to import anything inside it
__init__.py ----------------- A file required for Python to treat the demosite directory as a package.
settings.py ----------------- Settings/configuration for this Django project
urls.py ---------------------- Root URL config, the URLs for this Django project, provides mapping to views
wsgi.py ---------------------- An entry-point for WSGI-compatible webservers to serve your project
demoapp/ -----------------
__init__.py --------
migrations/ -----as a version control of database schema. Packaging up your model change into individual migration files.
urls.py ------------ URL Configuration of your app, include in Root URL.
views.py ---------- Responsible for processing a user’s request and for returning the response
conf.py --------- Separate Settings of your own app. Can override by project settings. By using django-appconf.
models.py --------- A model is the single, definitive source of information about your data.
admin.py ---------- It reads metadata in your model to provide a powerful and production-ready interface
forms.py ----------- To create and manipulate form data
static/demoapp/ --- Static file of your app. python manage.py collectstatic in production, copy all to STATIC_ROOT.
templates/demoapp/ ----------- Templates file of your app. In project, loader.get_template(‘demoapp/index.html’)
templates/ ----------------- HTML files , renders based on views. You can change to any dir, configurable in settings.py
static/ ----------------------- CSS, JS, images.. etc, configurable in settings.py
17. 17
Features
Object Relational Mapper – ORM
MVC (MVTC) Architecture
Focuses on automating as much as possible and adhering to the DRY principle
Template System
Out of the box customizable Admin Interface, makes CRUD easy
Built-in light weight Web Server
Elegant URL design
Reverse resolution of URLS.
Custom Middleware
Authentication / Authorization
Internationalization, Time Zone support
Cache framework, with multiple cache mechanisms
Free, and Great Documentation
18. 18
Why use django?
Everything is Python Package
Django, Third Party Packages, Your Application, Your Site
monkey patch.(simply the dynamic replacement of attributes at runtime.)
Virtualenv虚拟环境搭建.
Pip 强大的库管理.setup.py,自由发布.
Batteries.(自带电池,各种第三方库.)
db, forms, templates, views, middleware, test
migrations, auth, admin, cache, sessions, gis.
Bench: jinjia2, SQLALchemy, Rhetoric, django-xadmin, South.
文档齐全,活跃的社区.
https://www.djangopackages.com/
https://www.djangoproject.com/
http://www.django-rest-framework.org/
https://djangosnippets.org/
24. 24
Questions
Q:
Have you considered using Django and found good reasons not to do so?
A:
Yeah, I am an honest guy, and the client wanted to charge by the hour.
There was no way Django was going to allow me to make enough money.