Title: ??????????????????????? Django
1??????????????????????? Django
- ??????????????????????????????????????(01204223)
??.??.????? ?????? ??????????????????????????????
????????????? ??????????????????????
2????? Django
"The Web Framework for Perfectionists with
Deadlines"
- ???????????????????????????????????
- ?????????????????? 2.x (???????????? 3.x)
- ???????????????????????? Django
- Washington Post (http//www.washingtonpost.com)
- Guadian.co.uk
- E-Labsheet (http//cloud3.cpe.ku.ac.th/elab)
3?????? HTML
- ???????????????????????? (Tag)
- ??????????????????????????????????????????????????
?????????????
????????
???????
lthtmlgt ltheadgt lttitlegtExamplelt/titlegt
lt/headgt ltbodygt lth1gtHellolt/h1gt Welcome
to my homepage. lt/bodygt lt/htmlgt
???? html
??????????????????
lthtmlgtltheadgtlttitlegtExamplelt/titlegtlt/headgt ltbodygtlth
1gtHellolt/h1gtWelcome to my homepage.lt/bodygtlt/htmlgt
4???????????????????? Django
Client (Web browser)
Server (Web App)
Database
5????????????????
- ???????????????? myweb
- ???????????? myweb ????????? ???? shape
- ???????? ????????????????????????????????????
django-admin startproject myweb cd myweb
./manage.py startapp shape
6???????? ????????????????
- ????????? settings.py ???????? shape
??????????????????????????????
INSTALLED_APPS ( 'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sit
es', '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)
7???????????
- ???????????? index ?? shape/views.py
- ???????? ??????????????????????? request
???????????????????????
from django.http import HttpResponse def
index(request) return HttpResponse('Hello')
(shape/views.py)
8????????????????? (/)
- ?????????? / ?????????????????????? 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() urlpatterns patterns('',
url(r'', 'shape.views.index'),
Example (r'first/', include('first.foo.url
s')), 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)), )
regular expression
(urls.py)
9???????????????
- ?????????????????????? manage.py
- ???? http//127.0.0.18000/ ???????? Location
???????????????? - ????????? http//08000 ????????????
./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.18000/ Quit the server
with CONTROL-C.
10???????
- ?????????????????????? ??????????????????
HttpResponse ????????????????????????????? - ????????????? tag ???? ? ??????
- Django ?????????????????? HttpResponse ???
"???????" (Template) ???????????????????? - ??????????????????????????? HTML
- ??? Web Designer ???????????????
?????????????????? - ???????????????????????????????????? HTML Editor
- ???? DreamWeaver, Kompozer
11???????????? index.html
- ?????????????????????????
- ??????????? templates/index.html ???????
(?????????? Kompozer ????????????? HTML Editor
???????)
mkdir templates
(templates/index.html)
????????? H1
???????????????? shape/rectangle/, shape/circle/
??? shape/triangle/ ????????
?????????????
Horizontal Bar
12???????????????????
- ????????? settings.py ??????????????????????????
import os PROJECT_DIR os.path.dirname(__file__)
TEMPLATE_DIRS ( os.path.join(PROJECT_DIR
, 'templates'), )
(settings.py)
13????????????????????????
- ????????????? index ?? shape/views.py
- ????????? render_to_response ???????????????
HttpResponse ?????????? - ????????????????????????????????
from django.http import HttpResponse from
django.shortcuts import render_to_response def
index(request) return render_to_response('ind
ex.html')
(shape/views.py)
14HTML Form
- ?????? HTML ??????????????????????????????????????
?
15?????????????? /shape/rectangle
- ????? 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)
16???????? rectangle_form
- ??????????????????? views.py
def rectangle_form(request) return
render_to_response('rectangle.html')
(shape/views.py)
17?????????? rectangle.html ???????????
- ????????????????????? /shape/rectangle/
- ??????? Action ???? calculate ?????????? Submit
????????????????? /shape/rectangle/calculate
???? width
???? height
18????????????????????????
- ??????????????????????????????????????????????????
????????????????????? ????? ?????????? ??? - ???????????????? result.html ??????
??????????????????????????????????
(templates/result.html)
19???????????????????
????????????????????????????????
def calculate(request, shape_type) if
shape_type 'rectangle' w
float(request.POST'width') h
float(request.POST'height') circum
2(wh) area wh return
render_to_response('result.html',
'area' area, 'circum' circum )
(shape/views.py)
???????????????????????????????
20??????????????
- ??????????????????????????????????????
- /shape/rectangle/calculate
- /shape/circle/calculate
- /shape/triangle/calculate
- ???????????????????? urls.py
urlpatterns patterns('', url(r'',
'shape.views.index'), url(r'shape/rectangle/
', 'shape.views.rectangle_form'),
url(r'shape/(.)/calculate', 'shape.views.calcul
ate'), )
(urls.py)
??????????????????????????????????????????????
21??????????? CSRF Protection
- Django ????????????? Cross-Site Request Forgery
(CSRF) ???????????????????????????????? - ??????????????????? calculate
- ???????????????????????????????????????
- ????? shape/views.py ?????? _at_csrf_except
decorator ?????? calculate
from django.views.decorators.csrf import
csrf_exempt _at_csrf_exempt def
calculate(request, shape_type)
(shape/views.py)
22?????????/???????
- ????????????????????????????????????????
- ????? ???????????
- ?????????? ????????????????????????