Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Dec 31, 2014

How to use request.PUT or request.DELETE in Django

Those of you who use Django might know that using request.PUT or request.DELETE on Django doesn't work. This is because Django doesn't construct dictionaries for such "verbs", and you need to work with request.raw_post_data or request.body and parse it to get your data (Go ahead, give it a try, while I have a cookie!)

If you are creating a simple REST API, you are not going to have a good time unless you are working with a framework.

Wouldn't it be nice if you could use request.PUT just like the dicts request.POST and request.GET After finally searching online, I have stumbled upon a way to do just that.

You can find the code on BitBucket.

    if request.method == "PUT":
        if hasattr(request, '_post'):
            del request._post
            del request._files
       
        try:
            request.method = "POST"
            request._load_post_and_files()
            request.method = "PUT"
        except AttributeError:
            request.META['REQUEST_METHOD'] = 'POST'
            request._load_post_and_files()
            request.META['REQUEST_METHOD'] = 'PUT'
           
        request.PUT = request.POST

What you do here is simply change the request method back to POST, reload the variables and rename the request method and request.POST dictionary. A nice hack to get your job done.

Liked this post? Have any suggestions? Just let me know. Feel free to comment below!

Jul 4, 2013

Programming - The Django Way!

When I started web development, PHP seemed the easiest language (and the obvious choice) to get little things done. I would first submit simple data through a form and store it in a database. Eventually, with that knowledge and a bit of AJAX, I came up with a full application, which worked well, but only I knew deep down how bad my procedural programming was. That was two years ago.

Soon thereafter, I shifted to Django for my needs and life has been great ever since. And I wonder it took me two years to write a blog post on the same.

Jan 30, 2012

A break from blogging!

It has been nine months since I started this blog. Count the number of posts, and it averages out to around a post a week. However, my last post was almost four weeks ago. Firstly, I do not have any exams going on and secondly, today is the fifth holiday continuously! As I have not come up with anything of late, I decided it was time to ponder why I am finding it hard to think of anything to write.