voldyman
voldyman

Reputation: 317

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(self):
   #double cool

it would be super cool

Upvotes: 0

Views: 189

Answers (1)

Benjamin Wohlwend
Benjamin Wohlwend

Reputation: 31808

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

Upvotes: 3

Related Questions