Django The Web Framework for Perfectionists with Deadlines
แนะนำ Django "The Web Framework for Perfectionists with Deadlines" l เฟรมเวรคสำหรบสรางเวบแอพลเคชน l พฒนาดวยภาษาไพธอน 2. x (ยงไมรองรบ 3. x( l ตวอยางเวบทสรางดวย ¡Washington Django Post (http: //www. washingtonpost. com) ¡Guadian. co. uk ¡E-Labsheet (http: //cloud 3. cpe. ku. ac. th/elab) 2
การตอบสนองคำรองขอใน Django http: //mysite. com/path/to/1/2 Client (Web browser) Server (Web App) Request Response <app>/models. py Models URL resolver Views urls. py <app>/views. py Database Templates templates/*. html 4
ระบแอพฯ ทตองการใชงาน l แกไขไฟล โดยเพม เขาไปในรายการแอพลเคชนทใช settings. py shape : INSTALLED_APPS = ( 'django. contrib. auth', 'django. contrib. contenttypes', 'django. contrib. sessions', 'django. contrib. sites', 'django. contrib. messages', 'shape', # Uncomment the next line to enable the admin: # 'django. contrib. admin', # Uncomment the next line to enable admin documentation: # 'django. contrib. admindocs', ) (settings. py) 6
สรางววแรก l สรางววชอ index ใน shape/views. py from django. http import Http. Response def index(request): return Http. Response('Hello') (shape/views. py) l หมายเหต : ทกววฟงกชนจะตองรบ เปนอารกวเมนตแรกเสมอ request 7
กำหนดววใหรทพาธ (/) l ระบใหพาธ / ถกสงไปประมวลผลโดยวว index โดยแกไขไฟล urls. py from django. conf. urls. defaults import * # Uncomment the next two lines to enable the admin: # from django. contrib import admin # admin. autodiscover() regular expression urlpatterns = patterns('', url(r'^$', 'shape. views. index'), # Example: # (r'^first/', include('first. foo. urls')), # Uncomment the admin/doc line below to enable admin documentation: # (r'^admin/doc/', include('django. contrib. admindocs. urls ')), # Uncomment the next line to enable the admin: # (r'^admin/', include(admin. site. urls)), ) (urls. py) 8
ทดสอบแอพลเคชน l รนเวบเซรฟเวอรผาน manage. py $. /manage. py runserver Validating models. . . 0 errors found Django version 1. 2. 5, using settings 'myweb. settings' Development server is running at http: //127. 0. 0. 1: 8000/ Quit the server with CONTROL-C. l กรอก http: //127. 0. 0. 1: 8000/ Location ของเวบบราวเซอร ¡หรอพมพ ลงในชอง http: //0: 8000 กไดเชนกน 9
กำหนดทเกบเทมเพลท l แกไขไฟล settings. py เพอระบทอยของเทมเพลท import os PROJECT_DIR = os. path. dirname(__file__) : TEMPLATE_DIRS = ( os. path. join(PROJECT_DIR, 'templates'), ) (settings. py) 12
เรยกใชงานเทมเพลทจากวว l แกไขฟงกชน ¡ใชทางลด index ใน shape/views. py render_to_response สรางออบเจกต Http. Response จากเทมเพลท from django. http import Http. Response from django. shortcuts import render_to_response def index(request): return render_to_response('index. html') l รโหลดหนาเวบเพอดความถกตอง (shape/views. py) 13
กำหนดววใหพาธ /shape/rectangle l เพม URL mapping ใน urls. py from django. conf. urls. defaults import * # Uncomment the next two lines to enable the admin: # from django. contrib import admin # admin. autodiscover() urlpatterns = patterns('', url(r'^$', 'shape. views. index'), url(r'^shape/rectangle/$', 'shape. views. rectangle_form '), # Example: # (r'^first/', include('first. foo. urls')), # Uncomment the admin/doc line below to enable admin documentation: # (r'^admin/doc/', include('django. contrib. admindocs. urls ')), # Uncomment the next line to enable the admin: # (r'^admin/', include(admin. site. urls)), ) (urls. py) 15
สรางวว rectangle_form l เพมฟงกชนลงไปใน views. py def rectangle_form(request): return render_to_response('rectangle. html') (shape/views. py) 16
สรางววสำหรบคำนวณ l เพมฟงกชนใน views. py �������� def calculate(request, shape_type): if shape_type == 'rectangle': w = float(request. POST['width']) h = float(request. POST['height']) circum = 2*(w+h) area = w*h return render_to_response('result. html', { 'area' : area, 'circum' : circum }) (shape/views. py) ���������� 19
กำหนดววใหพาธ l เราจะใหพาธตอไปนถกสงไปววเดยวกน ¡ /shape/rectangle/calculate ¡ /shape/circle/calculate ¡ /shape/triangle/calculate l ระบนพจนเรกลารใน urls. py urlpatterns = patterns('', url(r'^$', 'shape. views. index'), url(r'^shape/rectangle/$', 'shape. views. rectangle_form'), url(r'^shape/(. *)/calculate$', 'shape. views. calculate'), ) ���������������� (urls. py) 20
ระงบการใช CSRF Protection l Django มระบบปองกน Cross-Site Request Forgery (CSRF) ใหกบทกววทรบขอมลจากฟอรม ¡ ในตวอยางนคอวว calculate ¡ เราจะระงบการใชงานไปกอนเพอความสะดวก l แกไข shape/views. py โดยใส @csrf_except decorator ใหกบ calculate from django. views. decorators. csrf import csrf_exempt : @csrf_exempt def calculate(request, shape_type): : (shape/views. py) 21
- Slides: 22