""" ========================== James Crasta's RestfulView ========================== `As posted on Google Groups`__: .. __: http://groups.google.com/group/django-developers/msg/5f066b7ce2197e82 ----- Usage ----- Define a class which extends RestfulView in one of your views:: class BlogComments(RestfulView): def _find_post(self, request, year, month, day, slug): " Helper function used in all the comment views " try: return Post.objects.find_post(year, month, day, slug) except Post.DoesNotExist: raise Http404() def handle_GET(self, request, year, month, day, slug): post = self._find_post(year, month, day, slug) posts = post.comment_set.filter(previewed=True) return render_to_response('blog/comments.html', {'posts': posts} def handle_POST(self, request, year, month, day, slug): post = self._find_post(year, month, day, slug): # do AddManipulator stuff here return render_to_response(..... ) Now, either instantiate the class in the view:: comments = BlogComments() # this line goes after the class definition in your view ...then call that instance in your urlconf:: urlpatterns = patterns('', (r'^archive/(?P\d+)/(?P\d+)/(?P\d+)/(?P[\w-]+)/comments/', 'myapp.blog.views.comments') Or call it from your urlconf directly:: from myapp.views.comments import BlogComments urlpatterns = patterns('', (r'^archive/(?P\d+)/(?P\d+)/(?P\d+)/(?P[\w-]+)/comments/', BlogComments()) ----------- Usage Hints ----------- You can use Django decorators if you forgo the class method approach and use static functions. Obviously, you cannot reference ``self`` if you do this. Here's a quick hint on possible usage with the above static function and newforms:: class TheClassName(RestfulView): @staticmethod def handle_GET(request, form=YourFormName()): # ... @staticmethod @login_required def handle_POST(request): form = YourFormName(request.POST) if form.is_valid(): # ... else: return TheClassName.handle_GET(request, form=form) """ from django.http import HttpResponseNotAllowed class RestfulView(object): def __call__(self, request, *args, **kw): # try and find a handler for the given HTTP method method = request.META['REQUEST_METHOD'].upper() handler = getattr(self, 'handle_%s' % method, None) if handler is None: # compile a list of all our methods and return an HTTP 405 methods = [] for x in dir(self): if x.startswith('handle_'): methods.append(x[7:]) return HttpResponseNotAllowed(methods) return handler(request, *args, **kw)