Class that takes a normal none derived class and converts it into a view. The methods return simple datastructures which makes it easier to test.
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
- find even number by Rajeev529 2 weeks, 4 days ago
- Form field with fixed value by roam 1 month, 1 week ago
- New Snippet! by Antoliny0919 1 month, 2 weeks ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 4 months ago
- get_object_or_none by azwdevops 7 months, 4 weeks ago
Comments
Please login first before commenting.