Learning django step 1

2,625 views

Published on

Published in: Software
0 Comments
32 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,625
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
117
Comments
0
Likes
32
Embeds 0
No embeds

No notes for slide

Learning django step 1

  1. 1. 30 分鐘建構 
 Web App Cd Chen http://www.cdchen.idv.tw/ 2014/04/14 Learning Django Step 1
  2. 2. A
 b
 o
 u
 t
 陳永昇 (Cd Chen) http://www.cdchen.idv.tw/ 
 學歷:國⽴立台中科技⼤大學 經歷: 聯成電腦講師 恆逸資訊講師 現職: 乃師實業技術總監 passpass.cc 創辦⼈人 證照: RHCE / LPIC / NCLP MCSA / MCSE OCPJP / OCPJWCD TCSE / NSPA
  3. 3. LCURD List / Create / Update / Read / Delete
  4. 4. X 交給 Django 內建管理主控台 X X X
  5. 5. Post List Page
  6. 6. Post Detail Page
  7. 7. Post List
 <BASE_URL>/post/" Post Detail
 <BASE_URL>/post/<POST_ID>/
  8. 8. MVC
  9. 9. Controller View Model 控制程式的流程
  10. 10. Controller View Model 提供互動界⾯面
  11. 11. Controller View Model 存取資料
  12. 12. Controller View Model
  13. 13. Controller View Model
  14. 14. Controller View Model
  15. 15. Controller View Model View Template MVC in Django
  16. 16. 建⽴立 Django App
  17. 17. 1. 建⽴立 Django Project 2. 建⽴立 Django App 3. 撰寫程式 4. 設定與測試
  18. 18. 建⽴立 Django Project
  19. 19. 建⽴立 Django Project (postapp)cd-macmini1:postapp cdchen$ django-admin.py startproject demosite" (postapp)cd-macmini1:postapp cdchen$ ls" bin demosite include lib" (postapp)cd-macmini1:postapp cdchen$ cd demosite/" (postapp)cd-macmini1:demosite cdchen$ ls" demosite manage.py" (postapp)cd-macmini1:demosite cdchen$ cd demosite/" (postapp)cd-macmini1:demosite cdchen$ ls" __init__.py settings.py urls.py wsgi.py" (postapp)cd-macmini1:demosite cdchen$ cd .." (postapp)cd-macmini1:demosite cdchen$
  20. 20. Django Project 架構 ‣ settings.py ‣ Django 專案的設定檔 ‣ urls.py ‣ URL Mapping 定義檔 ‣ wsgi.py ‣ Django WSGI Callback
  21. 21. 建⽴立 Django App
  22. 22. 建⽴立 Django App (postapp)cd-macmini1:demosite cdchen$ ls" demosite manage.py" (postapp)cd-macmini1:demosite cdchen$ ./manage.py startapp postapp" (postapp)cd-macmini1:demosite cdchen$ ls" demosite manage.py postapp" (postapp)cd-macmini1:demosite cdchen$ ls postapp/" __init__.py admin.py models.py tests.py views.py" (postapp)cd-macmini1:postapp cdchen$
  23. 23. Django App 架構 ‣ admin.py ‣ 定義 Django 管理主控台 ‣ models.py ‣ 定義 Model ‣ tests.py ‣ 單元測試
  24. 24. 撰寫程式
  25. 25. 定義 Model from django.db import models" " # Create your models here." " class Post(models.Model):" title = models.CharField(max_length=255)" body = models.TextField(null=True, blank=True)" create_time = models.DateTimeField(db_index=True, auto_now_add=True)"
  26. 26. 撰寫 View ‣ Function-based View ‣ Generic View Class
  27. 27. # -*- coding: utf-8 -*-" from django.conf.urls import patterns, url" from django.views.generic import ListView, DetailView" from postapp.models import Post" " " class PostListView(ListView):" model = Post" template_name = 'post/list.html'" " " class PostDetailView(DetailView):" model = Post" template_name = 'post/detail.html'" " " ## 定義 URL Mapping" urlpatterns = patterns(''," url(r’^(?P<pk>d+)$', PostDetailView.as_view(), name='post_detail_view')," url(r'^$', PostListView.as_view(), name='post_list_view')," )"
  28. 28. 撰寫 Template ‣ Template 的位置 ‣ App 中 ‣ 獨⽴立的路徑 ‣ Template 語法 ‣ Template Tag ‣ Template Filter
  29. 29. post/list.html <!DOCTYPE html>" <html>" <head><title>Post List Page</title></head>" <body>" <h1>Post List</h1>" <ul>" {% for object in object_list %}" <li>" <small>{{ object.create_time }}</small>" <h2><a href="{% url 'post_detail_view' pk=object.pk %}">{{ object.title }}</a></h2>" <div>{{ object.body|truncatechars:30 }}</div>" </li>" {% endfor %}" </ul>" </body>" </html>
  30. 30. post/detail.html <!DOCTYPE html>" <html>" <head>" <title>{{ object.title }}</title>" </head>" <body>" <h1>{{ object.title }}</h1>" <small>{{ object.create_time }}</small>" <div>" {{ object.body }}" </div>" </body>" </html>
  31. 31. 撰寫 admin.py from django.contrib import admin" from postapp.models import Post" " " class PostModelAdmin(admin.ModelAdmin):" pass" " " admin.site.register(Post, PostModelAdmin)"
  32. 32. 設定與測試
  33. 33. 設定 ‣ settings.py ‣ INSTALLED_APPS ‣ DATABASES ‣ urls.py
  34. 34. INSTALLED_APPS INSTALLED_APPS = (" 'django.contrib.admin'," 'django.contrib.auth'," 'django.contrib.contenttypes'," 'django.contrib.sessions'," 'django.contrib.messages'," ‘django.contrib.staticfiles'," ‘postapp’," )
  35. 35. DATABASES DATABASES = {" 'default': {" 'ENGINE': 'django.db.backends.sqlite3'," 'NAME': os.path.join(BASE_DIR, 'db.sqlite3')," }" }
  36. 36. urls.py from django.conf.urls import patterns, include, url" from django.contrib import admin" admin.autodiscover()" urlpatterns = patterns(''," # Examples:" # url(r'^$', 'demosite.views.home', name='home')," # url(r'^blog/', include('blog.urls'))," url(r'^admin/', include(admin.site.urls))," url(r'^post/', include('postapp.views'))," )
  37. 37. 建⽴立資料庫 ‣ 預設使⽤用 SQLite 作為資料庫 ‣ 可搭配使⽤用 django-south 管理 Model 欄 位 ‣ Django 1.7 將直接內建 ‣ 執⾏行 manage.py syncdb
  38. 38. (postapp)cd-macmini1:demosite cdchen$ ./manage.py syncdb" Creating tables ..." Creating table django_admin_log" Creating table auth_permission" Creating table auth_group_permissions" Creating table auth_group" Creating table auth_user_groups" Creating table auth_user_user_permissions" Creating table auth_user" Creating table django_content_type" Creating table django_session" Creating table postapp_post" " You just installed Django's auth system, which means you don't have any superusers defined." Would you like to create one now? (yes/no): yes" Username (leave blank to use 'cdchen'): admin" Email address: admin@localhost.cc" Password: <PASSWORD>" Password (again): <PASSWORD>" Superuser created successfully." Installing custom SQL ..." Installing indexes ..." Installed 0 object(s) from 0 fixture(s)" (postapp)cd-macmini1:demosite cdchen$
  39. 39. 測試 ‣ Django 內建測試伺服器 ‣ 可搭配 django-devserver ‣ 執⾏行:./manage.py runserver
  40. 40. (postapp)cd-macmini1:demosite cdchen$ ./manage.py runserver" Validating models..." " 0 errors found" April 13, 2014 - 08:36:13" Django version 1.6.2, using settings 'demosite.settings'" Starting development server at http://127.0.0.1:8000/" Quit the server with CONTROL-C."
  41. 41. http://localhost:8000/admin/
  42. 42. http://localhost:8000/post/
  43. 43. http://localhost:8000/post/1
  44. 44. 下⼀一步??
  45. 45. ‣ 更複雜的 ORM 機制 ‣ 熟悉 Form / Function-Based View ‣ 使⽤用 3rd-party App ‣ ⾃自定 Template Tag / Filter ‣ 開發 Reusable-App
  46. 46. niceStudio http://www.niceStudio.com.tw/
  47. 47. 報告完畢
 敬請指教

×