- # urls.py
- from django.conf.urls.defaults import *
- from mysite import views
- urlpatterns = patterns('',
- # ...
- (r'^somepage/$', views.some_page),
- # ...
- )
- # views.py
- from django.http import Http404, HttpResponseRedirect
- from django.shortcuts import render_to_response
- def some_page(request):
- if request.method == 'POST':
- do_something_for_post()
- return HttpResponseRedirect('/someurl/')
- elif request.method == 'GET':
- do_something_for_get()
- return render_to_response('page.html')
- else:
- raise Http404()
- # 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)
- elif request.method == 'POST' and POST is not None:
- return POST(request)
- 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}),
- # ...
- )
- def method_splitter(request, *args, **kwargs):
- get_view = kwargs.pop('GET', None)
- post_view = kwargs.pop('POST', None)
- if request.method == 'GET' and get_view is not None:
- return get_view(request, *args, **kwargs)
- elif request.method == 'POST' and post_view is not None:
- return post_view(request, *args, **kwargs)
- raise Http404
- def foo(*args, **kwargs):
- print "Positional arguments are:"
- print args
- print "Keyword arguments are:"
- print kwargs
- >>> foo(1, 2, 3)
- Positional arguments are:
- (1, 2, 3)
- Keyword arguments are:
- {}
- >>> foo(1, 2, name='Adrian', framework='Django')
- Positional arguments are:
- (1, 2)
- Keyword arguments are:
- {'framework': 'Django', 'name': 'Adrian'}
新闻热点
疑难解答