Upload
buidiep
View
218
Download
0
Embed Size (px)
Citation preview
1
����������� �������������������� ��������� DjangoDjango
�������ก�ก�� �ก������ก�����������������ก�ก�� �ก������ก����������(01204223)(01204223)
��.�.�� � !"#ก$�%����������ก����������
�&'����ก���(�� �)���* �+� ก,���(��
2
����� Django
� -�����(.�)��($�/�0�#��+����1���21��$� %�,�3�4�1 2.x ( �/3�6�/�� 3.x)����� 6�/�0�*��($�/�$� Django
�Washington Post (http://www.washingtonpost.com)�Guadian.co.uk�E-Labsheet (http://cloud3.cpe.ku.ac.th/elab)
"The Web Framework for Perfectionists with Deadlines"
3
��ก��� HTML��'ก���$� S$�����#+'#*0ก (Tag)
��6�/�6�/*����กก�6�)1U�/�6�/#+'ก�SUV1�*��!)�63�6���+�6��ก(�
<html><head>
<title>Example</title></head><body>
<h1>Hello</h1>Welcome to my homepage.
</body></html>
�#$�%�ก #$�%�ก
���� html ���� ������������
<html><head><title>Example</title></head><body><h1>Hello</h1>Welcome to my homepage.</body>< /html>
4
ก��&����'������'(�)� Django
Database
Views
<app>/views.py
URL resolver
urls.py
Models
<app>/models.py
Templates
templates/*.html
Request Response
Client (Web browser)
Server (Web App)
http://mysite.com/path/to/1/2
5
����'*#��+��),-.�($�/W�"0��X�� myweb�%� !1W�"0� myweb ($�/#��Z �X�� shape
���������: ��������� !"��#$%�&�'(����)���*�%+��
$ django-admin startproject myweb$ cd myweb$ ./manage.py startapp shape
6
��/��0 %12&��'ก��)��'���#ก$3S3-+� settings.py W� ���� shape S$�3�
!1� ก�#��+����1*��!�$: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 documenta tion:# 'django.contrib.admindocs',
)
(settings.py)
7
����'� ��ก�($�/����X�� index !1 shape/views.py
� ��������: ,�ก�*��. ก�+��#/�(� ��� request �"5�����ก*������)�ก�6��
from django.http import HttpResponse
def index(request):return HttpResponse('Hello')
(shape/views.py)
8
ก��,��� �),��3%��4 (/)�'�[!)$��4 / \]ก(6/3��'��+�+W� ��� index
W� #ก$3S3-+� urls.pyfrom 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'),# Example:# (r'^first/', include('first.foo.urls')),
# Uncomment the admin/doc line below to enable admi n documentation:# (r'^admin/doc/', include('django.contrib.admindoc s.urls')),
# Uncomment the next line to enable the admin:# (r'^admin/', include(admin.site.urls)),
)
regular expression
(urls.py)
9
%������ ������ �1�0�_��-����6�1 manage.py
�ก�ก http://127.0.0.1:8000/ +/!1�6�/ Location S�/�0����_���)X������ http://0:8000 ก03�$�61ก�1
$ ./manage.py runserverValidating models...0 errors found
Django version 1.2.5, using settings 'myweb.settings 'Development server is running at http://127.0.0.1:8 000/Quit the server with CONTROL-C.
10
�%-���%� (.�)���ก(�*��_��_$�1 ก�($�/�0��"0ก�� HttpResponse
W� �/"�ก����d1X��/ [6/ �ก��$�/(��/����� tag �6�/ e ���/
� Django ��*�/+��!1ก�($�/ HttpResponse "�ก "*��+*" (Template) *���� �3�$+6�/)1$�
� *��+*��+�ก,&')�X�1�ก(� HTML� !)$ Web Designer �d1�]$($�/!)$ 3�6!�6W�#ก�����($�/3�$('��กW� ���� _�-��#���'%* HTML Editor
� �61 DreamWeaver, Kompozer
11
����'�%-���% index.html� �� �3�����!)$*��+*
� ($�/�ก(� templates/index.html ��/#(�/ (!�$W�#ก� Kompozer )X�_�-��#�� HTML Editor !�ก03�$)
$ mkdir templates
!�$]�#�� H1 ($�/+�V/��3�*�� shape/rectangle/, shape/circle/ #+' shape/triangle/
���+.����
Horizontal Bar!(6�X������/
(templates/index.html)
12
ก��,��%12�ก��%-���%�#ก$3S3-+� settings.py �X��'�[*��� ]6S�/*��+*
import os
PROJECT_DIR = os.path.dirname(__file__):
TEMPLATE_DIRS = (os.path.join(PROJECT_DIR, 'templates'),
)
(settings.py)
13
��1�ก)��'���%-���%+�ก� ��#ก$3S-m/ก���1 index !1 shape/views.py
�!�$*�/+�� render_to_response ($�/�0��"0ก�� HttpResponse "�ก*��+*
��W)+�)1$��0��X���]����\]ก�$�/
from django.http import HttpResponsefrom django.shortcuts import render_to_response
def index(request):return render_to_response('index.html')
(shape/views.py)
15
ก��,��� �),���4 /shape/rectangle����� URL mapping !1 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_f orm'),# Example:# (r'^first/', include('first.foo.urls')),
# Uncomment the admin/doc line below to enable admi n documentation:# (r'^admin/doc/', include('django.contrib.admindoc s.urls')),
# Uncomment the next line to enable the admin:# (r'^admin/', include(admin.site.urls)),
)
(urls.py)
16
����'� � rectangle_form�����-m/ก���1+/3�!1 views.py
def rectangle_form(request):return render_to_response('rectangle.html')
(shape/views.py)
17
����'H��I- rectangle.html �#J��%-���%� ��4*��#(�/-���1�V�X� /shape/rectangle/
�ก�'�[ Action �d1 calculate *.�!)$ก�ก� Submit (6/S$��]+3� �/��4 /shape/rectangle/calculate
�X��: width
�X��: height
18
�&�1�-�%-���%��'K������L� �"'��ก#��!)$!�$*��+*#+'����� �ก�1(.�)��
ก��.�1�&ก�� �ก��(��)+�� � �/ก+� (��)+�� � Z+Z�($�/*��+*�X�� result.html ��/1�V
'�[�.�#)16/*��!)$���(6/�6���#(�/�+
(templates/result.html)
19
����'� ����,�������L� ����-m/ก���1!1 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 })
�6�)+6�1�V"'\]ก#*1*��!1*��+*
�����ก���1��'�[�1��S�/]�*/
(shape/views.py)
20
ก��,��� �),���4� �"'!)$��4�6�3�1�V\]ก(6/3������ �ก�1
�/shape/rectangle/calculate�/shape/circle/calculate �/shape/triangle/calculate
� '�[1��"1�ก]+��!1 urls.py
urlpatterns = patterns('',url(r'^$', 'shape.views.index'),url(r'^shape/rectangle/$', 'shape.views.rectangle_f orm'),url(r'^shape/(.*)/calculate$', 'shape.views.calcula te'),
)
S$�����(6�11�V"'\]ก(6/3��d1���ก���1��S�/���
(urls.py)
21
��'�ก��)�� CSRF Protection� Django ��'���r�/ก�1 Cross-Site Request Forgery
(CSRF) !)$ก��*[ก���*����S$��]+"�ก-���� !1���� 6�/1�V�X���� calculate� �"''/��ก�!�$/�13�ก6�1�X������('��ก
� #ก$3S shape/views.py W� !(6 @csrf_except decorator !)$ก�� calculatefrom django.views.decorators.csrf import csrf_exempt
:
@csrf_exemptdef calculate(request, shape_type):
:
(shape/views.py)