Top Banner
1/20 Django Tao He [email protected] @SELAB, SYSU
22

Django

May 06, 2015

Download

Technology

elfinhe
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Django

1/20

Django

Tao [email protected]

@SELAB, SYSU

Page 2: Django

2/20

Install

Install Python Set up a database Install Django

tar xzvf Django-NNN.tar.gz sudo python setup.py install

http://docs.djangoproject.com/en/dev/intro/install

Page 3: Django

3/20

Writing your first Django app

import django django-admin.py startproject mysite

mysite/ __init__.py manage.py 对 Django进行脚本管理 settings.py 项目配置 urls.py URL分配器配置

python manage.py runserver 启动开发服务器 wget -O- -q http://127.0.0.1:8000/ 测试服务器 python manage.py runserver 192.168.128.141:80

Page 4: Django

4/20

Database setup

vim settings.py 'ENGINE': 'django.db.backends.sqlite3‘ ‘NAME’:’mysite’

python manage.py syncdb

Page 5: Django

5/20

Creating models python manage.py startapp polls 创建 app Vim  polls/models.py

继承 django.db models.Model实现对应的配置接口

Page 6: Django

6/20

TIPS

Projects vs. apps A project can contain multiple apps. An app can be in multiple projects.

Philosophy A model is the single, definitive source of data about your

data. It contains the essential fields and behaviors of the data

you're storing. Django follows the DRY Principle. The goal is to define your data model in one place and

automatically derive things from it.

Page 7: Django

7/20

Activating models

Create a database schema (CREATE TABLE statements) for this app.

Create a Python database-access API for accessing Poll and Choice objects.

settings.py: INSTALLED_APPS  加上  'mysite.polls'

python manage.py sql polls python manage.py syncdb

Page 8: Django

8/20

Other cmds on DB

python manage.py sql polls 建立模型 python manage.py validate 验证models错误 python manage.py sqlcustom polls 自定义 SQL python manage.py sqlindexes polls 建立索引

SQL python manage.py sqlall polls 全部  sql

,sqlcustom, 和  sqlindexes  python manage.py dbshell 数据库控制台

Page 9: Django

9/20

Playing with the API

python manage.py shell 自动导入 mysite环境

Poll.objects.all() …

Page 10: Django

10/20

Activate the admin site

INSTALLED_APPS: ‘django.contrib.admin’ python manage.py syncdb 新 APP更新 DB vim urls.py 建立 url映射 python manage.py runserver开启服务器 manage.py createsuperuser 创建新用户 [polls]# vim admin.py 在 polls添加 admin.py

from mysite.polls.models import Poll from django.contrib import admin

admin.site.register(Poll)

…more admin ops

Page 11: Django

11/20

View

Views in Poll ‘archive’ page ‘detail’ page ‘results’ page Vote action

In Django, each view is a py function

Page 12: Django

12/20

Design URLs

Settings: ROOT_URLCONF = ‘mysite.urls’ 表示 mysite/urls.py

Find variable named urlpatterns in ROOT_URLCONF (regular expression, Python callback function [, optional dictionary])

Example: (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),

(?P<poll_id>) 定义一个命名组; (?P=name) 则是对命名组的逆向引用 

 \d+ 对应的正则表达式 /polls/23/

detail(request=<HttpRequest object>, poll_id=‘23’ ) Note : Django Will not search GET and POST parameters

Page 13: Django

13/20

First View

t = loader.get_template('polls/index.html') c = Context({ 'latest_poll_list':

latest_poll_list, }) HttpResponse(t.render(c)) A shortcut:

render_to_response(strTemplate , dicContext) PBL : ObjectDoesNotExist 为什么替换为

Http404 

Page 14: Django

14/20

Use the template system

The Django template language http://docs.djangoproject.com/en/dev/topics/

templates/#topics-templates

Page 15: Django

15/20

Decoupling the URLconfs

cp urls.py polls/ 去掉 ^polls/的部分和 admin的部分

Page 16: Django

16/20

POST GET

request.POST['choice']  request.GET '/polls/3/results/' ==

reverse(‘mysite.polls.views.results’, args=(p.id,) )

Page 17: Django

Model的自动转化

插入对象 mtable =MainTable(word_number="12.250") mtable.save() #此刻 word_number会转化成

12.250存储到数据库中 Python中除了 '' 、 "" 、 0 、 () 、 [] 、 {} 、

None 为 False之外,其他的都是 True。 

Page 18: Django

18/20

PBL——Model

Model建立的流程是怎样的呢? Model映射到数据库表时,如何处理外键? Model中的数据只能是基本的数据库类型么? Model按照数据库类型来声明会约束其应用么?会有不方便么?

我们项目如何在其之上开发出 Graph Database的应用? 如果仅仅使用映射表动态添加属性,性能会怎样? 常用的场景和操作有哪些?

全部属性检索 单个属性检索 组合属性检索

Django如何支持表连接操作?

Page 19: Django

19/20

PBL——View

怎样获取 Get 和 Post 怎样使用 Session 和 Cookie

Page 20: Django

20/20

PBL——Template

模板语言怎样使用? 循环,分支 安全性:跨站,注入?

Page 21: Django

21/20

PBL——Python语言

Meta-Programming

Page 22: Django

22/20

Thank you!