Class that converts object to view.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class View(object):
    def __init__(self, controller):
        self.controller = controller()

    def as_template(self, template):

        def view(request, *args, **kwargs):
            return TemplateResponse( request, template, self.handler(request, *args, **kwargs) )

        return view

    def handler(self, request, *args, **kwargs):
        method = request.method.lower()
        handler = getattr(self.controller, method)
        return handler(request, *args, **kwargs) 

#Use a none derived class with http methods that return simple data structures. 

class HomeView(object):
    def get(self, request):
       return { 'foo' : 'bar' }

#In your url confs you can specify what the view returns. Only as_template is available.
url( r'^$', View(HomeView).as_template('home.html'), name='homepage' )

More like this

  1. Easier chainability with custom QuerySets by bendavis78 1 year, 2 months ago
  2. View Permission Decorator Helper by jgeewax 4 years, 10 months ago
  3. Closure for FieldListFilter classes with custom sets of ranges by ssokolow 11 months ago
  4. Class-based process form view. by I159 1 year, 5 months ago
  5. Resource by zvoase 4 years, 8 months ago

Comments

(Forgotten your password?)