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!

Dec 11, 2014

Zico, the ISL and ATK

Zico might have been a great footballer, but the White Pele is perhaps one of the best players not to have won a World Cup ever. Maybe that's why he considers qualifying to the semi finals of the ISL as one of the greatest moments of his career. It's commendable how he has engineered the uphill task after a poor start by FC Goa to the tournament.

The moment I entered the Salt Lake Stadium, I knew the crowd wasn't going to let Zico go away with a victory, a result that would eliminate the home team Atletico de Kolkata out of the tournament. As the starting XI was being announced, the stadium cheered every name. The presence of 36 year old marquee signing Luis Garcia on the bench, raised doubts on his fitness, but Kolkata seemed to go with whatever they had.

Dec 10, 2014

And I'm back!

It's been a long time. A very long time indeed...

It's been so long that the once familiar Blogger interface seems alien to me. Due to my sabbatical, I must admit that I had thoughts of leaving this blog altogether and moving on to a new one, but recent events have convinced me otherwise.

Random people have asked me why I have stopped blogging. I had no idea so many people read this blog - and that they would miss my posts. But the one thing that wanted me to start writing again on this blog was during a job interview.

I had cleared the first level of interviews in a company, and the moment I walked into the room, one of the interviewers asked me - "How is your blog?" (The fact that I have neglected this blog in recent times might have contributed to me not making it to the said company.)

Apart from my readers reaching out to me in real life, I am going to start writing again because I am fresh out of the placement season at IIT Roorkee - a place where students are only as good as the packages they bag. I have a lot of things in mind - about the placement process, the selection procedure and a lot of other things - and I am going to share them on this blog soon (albeit in a safe way- to make sure I don't get in trouble with certain people).

To the readers, I would like to sincerely apologize because I have been away for a long time due to my own selfish reasons (the freelancing pays well, you know!) But watch this space, I am going to share my experience soon.

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