开发者

Django:Map urls to class like in web.py

i am learning django but gave web.py a try first. while reading django's documentation i found that in i need to check for the request type in each method.. like:

def myview():
  if request.method == "POST":
    #blah balh 
    #ke$ha (jst kiddn)
  else:
    #(balh)x2

can the web.py type classes be implemented in django like

class myView():
 def GET(self):
   #cool
 def POST开发者_如何学C(self):
   #double cool

it would be super cool


Yes, that's possible with the new (as in Django 1.3) class-based views:

from django.views.generic.base import View

class MyView(View):

    def get(self, request, *args, **kwargs):
        # return a response here

    def post(self, request, *args, **kwargs):
        # return a response here

Usually, you don't have to use the View base class, there are many views that are geared towards all kinds of cases, e.g. TemplateView or FormView. Reinout van Rees has two excellent blog posts that go into the details:

http://reinout.vanrees.org/weblog/2011/08/24/class-based-views-walkthrough.html

http://reinout.vanrees.org/weblog/2011/08/24/class-based-views-usage.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜