• Like
Django
Upcoming SlideShare
Loading in...5
×
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
594
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
5
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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