Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

generic.py 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from django.http import HttpResponseBadRequest
  2. class ModelView(object):
  3. u"""
  4. A generic view for models which can recieve GET and POST requests
  5. The __init__ method of subclasses should set the default response
  6. variable.
  7. """
  8. template_file = None
  9. response = None
  10. def __call__(self, request, *args, **kwargs):
  11. self.request = request
  12. self.args = args
  13. self.kwargs = kwargs
  14. method_name = "handle_%s" % request.method.upper()
  15. model = self.get_model()
  16. try:
  17. getattr(self, method_name)(model)
  18. except AttributeError:
  19. return HttpResponseBadRequest()
  20. return self.response
  21. def handle_GET(self, model):
  22. u"""Default implementation of model view is to do nothing."""
  23. pass
  24. def handle_POST(self, model):
  25. u"""
  26. Handle a POST request to this resource.
  27. This will forward on request to a method of form "do_%s" where the
  28. second part needs to be specified as an "action" name within the
  29. request.
  30. If you don't want to handle POSTs this way, just override this method
  31. """
  32. if 'action' in self.request.POST:
  33. getattr(self, "do_%s" % self.request.POST['action'].lower())(model)
  34. def get_model(self):
  35. u"""Responsible for loading the model that is being acted on"""
  36. return None