• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content







Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Django Django Presentation Transcript

    • Django Tao He elfinhe@gmail.com @SELAB, SYSU 1/20
    • Install  Install Python  Set up a database  Install Django  tar xzvf Django-NNN.tar.gz  sudo python setup.py installhttp://docs.djangoproject.com/en/dev/intro/install 2/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 测试服务器  python manage.py runserver 3/20
    • Database setup vim settings.py  ENGINE: django.db.backends.sqlite3‘  ‘NAME’:’mysite’ python manage.py syncdb 4/20
    • Creating models python manage.py startapp polls 创建 app Vim polls/models.py  继承 django.db models.Model  实现对应的配置接口 5/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 youre storing.  Django follows the DRY Principle.  The goal is to define your data model in one place and automatically derive things from it. 6/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 7/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 数据库控制台 8/20
    • Playing with the API python manage.py shell 自动导入 mysite 环 境 Poll.objects.all() … 9/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 10/20
    • View Views in Poll  ‘archive’ page  ‘detail’ page  ‘results’ page  Vote action In Django, each view is a py function 11/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 12/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 , dicContex t) PBL : ObjectDoesNotExist 为什么替换为 Http404 13/20
    • Use the template system The Django template language  http://docs.djangoproject.com/en/dev/topics/templates/ 14/20
    • Decoupling the URLconfs cp urls.py polls/ 去掉 ^polls/ 的部分和 admin 的部分 15/20
    • POST GET request.POST[choice] request.GET /polls/3/results/ ==  reverse(‘mysite.polls.views.results’, args=(p.id,) ) 16/20
    • Model 的自动转化 插入对象  mtable =MainTable(word_number="12.250")  mtable.save() # 此刻 word_number 会转化成 12.250 存储到数据库中 Python 中除了 、 "" 、 0 、 () 、 [] 、 {} 、 None 为 False 之外,其他的都是 True 。
    • PBL——Model Model 建立的流程是怎样的呢? Model 映射到数据库表时,如何处理外键? Model 中的数据只能是基本的数据库类型么? Model 按照数据库类型来声明会约束其应用么?会有不方 便么? 我们项目如何在其之上开发出 Graph Database 的应用? 如果仅仅使用映射表动态添加属性,性能会怎样? 常用的场景和操作有哪些?  全部属性检索  单个属性检索  组合属性检索 Django 如何支持表连接操作? 18/20
    • PBL——View 怎样获取 Get 和 Post 怎样使用 Session 和 Cookie 19/20
    • PBL——Template 模板语言怎样使用? 循环,分支 安全性:跨站,注入? 20/20
    • PBL——Python 语言 Meta-Programming 21/20
    • Thank you! 22/20