半尾 2010年06月12日 星期六 20:50 | 1438次浏览 | 0条评论
终于明白了
# views.py
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render_to_response
def method_splitter(request, GET=None, POST=None):
if request.method == 'GET' and GET is not None:
return
GET(request) #此处其实是return一个视图函数views.some_pages_get
elif request.method == 'POST' and POST is not None:
return
POST(request) #此处其实是return一个视图函数views.some_pages_post
raise Http404
def some_page_get(request):
assert request.method == 'GET'
do_something_for_get()
return render_to_response('page.html')
def some_page_post(request):
assert request.method == 'POST'
do_something_for_post()
return HttpResponseRedirect('/someurl/')
# urls.py
from django.conf.urls.defaults import *
from mysite import views
urlpatterns = patterns('',
# ...
(r'^somepage/$', views.method_splitter, {'GET': views.some_page_get, 'POST':
views.some_page_post}),
# ...
)
Python中有个有意思的事情就是:
def foobar(): pass
foo = foobar
那么foo()实际就是foobar()
一切 循环是迭代, 一切 对象是字典, 一切 命名是引用
一切 都是幻觉嘛
Zeuux © 2024
京ICP备05028076号
暂时没有评论