RestView - class for creating a view that dispatches based on request.method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Example usage:

    class ArticleView(RestView):
    
        def GET(request, article_id):
            return render_to_response("article.html", {
                'article': get_object_or_404(Article, pk = article_id),
            })

        def POST(request, article_id):
            # Example logic only; should be using django.forms instead
            article = get_object_or_404(Article, pk = article_id)
            article.headline = request.POST['new_headline']
            article.body = request.POST['new_body']
            article.save()
            return HttpResponseRedirect(request.path)

Then in your urls.py:

    from my_views import ArticleView
    
    urlpatterns = patterns('',
        ...
        (r'^article/(\d+)/$', ArticleView()),
        ...
    )

"""

from django.http import HttpResponse
import re

nonalpha_re = re.compile('[^A-Z]')

class RestView(object):
    """
    Subclass this and add GET / POST / etc methods.
    """
    allowed_methods = ('GET', 'PUT', 'POST', 'DELETE', 'HEAD', 'OPTIONS')
    
    def __call__(self, request, *args, **kwargs):
        method = nonalpha_re.sub('', request.method.upper())
        if not method in self.allowed_methods or not hasattr(self, method):
            return self.method_not_allowed(method)
        return getattr(self, method)(request, *args, **kwargs)
    
    def method_not_allowed(self, method):
        response = HttpResponse('Method not allowed: %s' % method)
        response.status_code = 405
        return response

More like this

  1. View dispatch based on HTTP verb by malcolmt 5 years, 7 months ago
  2. Using class methods as views by panyam 3 years, 11 months ago
  3. Simple views dispatcher by http methods by kmerenkov 3 years, 6 months ago
  4. Base class for RESTful Views by jpwatts 4 years, 5 months ago
  5. Resource by zvoase 4 years, 8 months ago

Comments

zvoase (on September 21, 2008):

This is all very well and good, but methods defined on the subclass also have to accept self as a positional argument, or be decorated by staticmethod. Also, having an actual self which is persistent across requests may provide risks to thread safety, etc.

I've implemented something similar, only it uses some magic to alleviate two problems:

  1. You don't have to instantiate the resource class in the URLconf
  2. You don't have to use staticmethod or self at any point in the method definitions.

You can get to that here

#

Harrigan (on September 21, 2008):

Exactly the same as Snippet 437.

#

simon (on September 22, 2008):

Hah! I was sure someone else had done this before (it's an obvious approach) but my Google-fu failed me.

#

a.v.khodyrev (on July 24, 2009):

How does one use this with 'reverse'?

#

shemigon (on November 14, 2009):

I solved this problem using url patterns with name parameter

#

(Forgotten your password?)