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
- Add custom fields to the built-in Group model by jmoppel 1 month, 1 week ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 4 months, 3 weeks ago
- Python Django CRUD Example Tutorial by tuts_station 5 months, 1 week ago
- Browser-native date input field by kytta 6 months, 3 weeks ago
- Generate and render HTML Table by LLyaudet 7 months ago
Comments
Please login first before commenting.