[Django]-How to implement restful webservice with python/django

6👍

There is also piston, which is a Django framework for creating RESTful APIs. It has a slight learning curve, but nicely fits into Django.

If you want something more lightweight, Simon Willison has very nice snippet that I have used previously that nicely models the HTTP methods:

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)

Jacob Kaplan-Moss has a nice article on Worst Practices in REST that can help guide you away from some common pitfalls.

1👍

Leave a comment