learning django step 1

48
30 分鐘建構 Web App Cd Chen http://www.cdchen.idv.tw/ 2014/04/14 Learning Django Step 1

Upload: -

Post on 14-May-2015

1.563 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Learning django step 1

30 分鐘建構 Web App

Cd Chen http://www.cdchen.idv.tw/

2014/04/14

Learning Django Step 1

Page 2: Learning django step 1

Ab o u t

陳永昇 (Cd Chen) http://www.cdchen.idv.tw/ 學歷:國⽴立台中科技⼤大學 經歷: 聯成電腦講師 恆逸資訊講師 現職: 乃師實業技術總監 passpass.cc 創辦⼈人 證照:

RHCE / LPIC / NCLP MCSA / MCSE OCPJP / OCPJWCD TCSE / NSPA

Page 3: Learning django step 1

LCURD

List / Create / Update / Read / Delete

Page 4: Learning django step 1
Page 5: Learning django step 1

X 交給 Django 內建管理主控台

XX X

Page 6: Learning django step 1

Post List Page

Page 7: Learning django step 1

Post Detail Page

Page 8: Learning django step 1

Post List

<BASE_URL>/post/"

Post Detail

<BASE_URL>/post/<POST_ID>/

Page 9: Learning django step 1

MVC

Page 10: Learning django step 1

Controller

View Model

控制程式的流程

Page 11: Learning django step 1

Controller

View Model

提供互動界⾯面

Page 12: Learning django step 1

Controller

View Model

存取資料

Page 13: Learning django step 1

Controller

View Model

Page 14: Learning django step 1

Controller

View Model

Page 15: Learning django step 1

Controller

View Model

Page 16: Learning django step 1

Controller

View Model

View

Template

MVC in Django

Page 17: Learning django step 1

建⽴立 Django App

Page 18: Learning django step 1

1. 建⽴立 Django Project

2. 建⽴立 Django App

3. 撰寫程式

4. 設定與測試

Page 19: Learning django step 1

建⽴立 Django Project

Page 20: Learning django step 1

建⽴立 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$

Page 21: Learning django step 1

Django Project 架構‣ settings.py

‣ Django 專案的設定檔

‣ urls.py

‣ URL Mapping 定義檔

‣ wsgi.py

‣ Django WSGI Callback

Page 22: Learning django step 1

建⽴立 Django App

Page 23: Learning django step 1

建⽴立 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$

Page 24: Learning django step 1

Django App 架構‣ admin.py

‣ 定義 Django 管理主控台

‣ models.py

‣ 定義 Model

‣ tests.py

‣ 單元測試

Page 25: Learning django step 1

撰寫程式

Page 26: Learning django step 1

定義 Modelfrom 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)"

Page 27: Learning django step 1

撰寫 View

‣ Function-based View

‣ Generic View Class

Page 28: Learning django step 1

# -*- 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'),")"

Page 29: Learning django step 1

撰寫 Template‣ Template 的位置

‣ App 中

‣ 獨⽴立的路徑

‣ Template 語法

‣ Template Tag

‣ Template Filter

Page 30: Learning django step 1

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>

Page 31: Learning django step 1

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>

Page 32: Learning django step 1

撰寫 admin.pyfrom django.contrib import admin"from postapp.models import Post"""class PostModelAdmin(admin.ModelAdmin):" pass"""admin.site.register(Post, PostModelAdmin)"

Page 33: Learning django step 1

設定與測試

Page 34: Learning django step 1

設定

‣ settings.py

‣ INSTALLED_APPS

‣ DATABASES

‣ urls.py

Page 35: Learning django step 1

INSTALLED_APPSINSTALLED_APPS = (" 'django.contrib.admin'," 'django.contrib.auth'," 'django.contrib.contenttypes'," 'django.contrib.sessions'," 'django.contrib.messages'," ‘django.contrib.staticfiles'," ‘postapp’,")

Page 36: Learning django step 1

DATABASES

DATABASES = {" 'default': {" 'ENGINE': 'django.db.backends.sqlite3'," 'NAME': os.path.join(BASE_DIR, 'db.sqlite3')," }"}

Page 37: Learning django step 1

urls.pyfrom 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')),")

Page 38: Learning django step 1

建⽴立資料庫

‣ 預設使⽤用 SQLite 作為資料庫

‣ 可搭配使⽤用 django-south 管理 Model 欄位

‣ Django 1.7 將直接內建

‣ 執⾏行 manage.py syncdb

Page 39: Learning django step 1

(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: [email protected]"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$

Page 40: Learning django step 1

測試

‣ Django 內建測試伺服器

‣ 可搭配 django-devserver

‣ 執⾏行:./manage.py runserver

Page 41: Learning django step 1

(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."

Page 42: Learning django step 1

http://localhost:8000/admin/

Page 43: Learning django step 1

http://localhost:8000/post/

Page 44: Learning django step 1

http://localhost:8000/post/1

Page 45: Learning django step 1

下⼀一步??

Page 46: Learning django step 1

‣ 更複雜的 ORM 機制

‣ 熟悉 Form / Function-Based View

‣ 使⽤用 3rd-party App

‣ ⾃自定 Template Tag / Filter

‣ 開發 Reusable-App

Page 47: Learning django step 1

niceStudioࠔҭࠁʃࣗ

http://www.niceStudio.com.tw/

Page 48: Learning django step 1

報告完畢敬請指教