python - Django GET and POST handling methods -
i want way automatically route , post requests subsequent methods in centralized way. want create handler in following way.
class myhandler(basehandler): def get(self): #handle requests def post(self): #handle post requests
this webapp2 , style, possible in django? want view in class-method style. kind of basehandler , router should write.
hint: use django generic views.
this supported in django class based views. can extend generic class view
, add methods get()
, post()
, put()
etc. e.g. -
from django.http import httpresponse django.views.generic import view class myview(view): def get(self, request, *args, **kwargs): return httpresponse('this request') def post(self, request, *args, **kwargs): return httpresponse('this post request')
the dispatch()
method view
class handles this-
dispatch(request, *args, **kwargs)
the view part of view – method accepts request argument plus arguments, , returns http response.
the default implementation inspect http method , attempt delegate method matches http method; delegated get(), post post(), , on.
by default, head request delegated get(). if need handle head requests in different way get, can override head() method. see supporting other http methods example.
the default implementation sets request, args , kwargs instance variables, method on view can know full details of request made invoke view.
then can use in urls.py
-
from django.conf.urls import patterns, url myapp.views import myview urlpatterns = patterns('', url(r'^mine/$', myview.as_view(), name='my-view'), )
Comments
Post a Comment