Declaring django views like web.py views

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django.http import HttpResponse as response
from django.http import HttpResponseNotAllowed

class ViewClass:
    def __call__(self, request, *args, **kwargs):
        self.request = request
        methods = ['POST', 'GET']
        self.methods = [method for method in dir(self)\
                 if callable(getattr(self, method)) and method in methods]

        if request.method in self.methods:
            view = getattr(self, request.method)
            return view(*args, **kwargs)
        else:
            return HttpResponseNotAllowed(self.methods)

class IndexView(ViewClass):
    def GET(self):
        return response("all ok %s" % self.request.method)

    def POST(self):
        return response("all ok %s" % self.request.method) 

index = IndexView()

More like this

  1. Allow separation of GET and POST implementations by agore 1 year, 10 months ago
  2. prevent GET or POST requests by jerzyk 6 years, 4 months ago
  3. Decorator that limits request methods by schinckel 4 years, 9 months ago
  4. Complex Form Preview by smagala 5 years ago
  5. View dispatch based on HTTP verb by malcolmt 6 years, 6 months ago

Comments

danigm (on February 5, 2010):

In urls.py the view is "index"

#

(Forgotten your password?)