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. Simple views dispatcher by http methods by kmerenkov 4 years, 5 months ago
  2. Using class methods as views by panyam 4 years, 10 months ago
  3. View dispatch based on HTTP verb by malcolmt 6 years, 6 months ago
  4. RESTful class dispatch by Phoenix 4 years, 10 months ago
  5. Resource by zvoase 5 years, 7 months ago

Comments

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?)