Take the 2-minute tour ×

To demostrate:

from django.views.generic.base import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator

class TestView(View):
        return HttpResponse('haha')

urls.py is

url(r'^test/', TestView.as_view()),

so when GET you can see haha, but when doing POST you get a blank page...

What am I missing here?

Edit: To clarify what I am doing. I am writing a JSON stream CURD view, which I need to parse JSON in various ways. One of them is when ppl POST data with certain pattern the view will dispatch to another method inside the view and return something. But it turns out returns nothing instead. So I present you the minimal PoC. Please help me what my code wenti wrong. TIA!

btw possible related question

share|improve this question

4 Answers 4

You need to implement its post method. See Class based views.

from django.http import HttpResponse
from django.views.generic import View

class TestView(View):

        # do something
        return super(TestView, self).dispatch(*args, **kwargs)

       # do somthing

       return HttpResponse('Hello, World!')

See more for dispatch docs.

The default implementation will inspect the HTTP method and attempt to delegate to a method that matches the HTTP method; a GET will be delegated to get(), a POST to post(), and so on.

So previously you was disturbing the above logic, calling super will allow dispatch to delegate to post method.

share|improve this answer
But I need to rewrite my own dispatch logic, and adding def post won't work either. –  est Mar 14 '13 at 4:24
@est See the updated answer! –  Ahsan Mar 14 '13 at 7:39
Perhaps I am wrong but what's the difference by directly return HttpResponse in dispatch() ? I provided a minimal POC in my question . Which in my case the dispatch handles more than get or post, but apparently no matter what I put in to HttpResponse it always returns empty body. –  est Mar 14 '13 at 7:52
I gave you the dispatch docs links, please read it first. –  Ahsan Mar 14 '13 at 8:20
Try a return in "do something" and POST the URL, you will get blank page... and I can' tell why even looking through the django source code. –  est Mar 14 '13 at 14:36

I know this doesn't really answer your question, but it may offer a workaround. What I have used in JSON applications is to send a post to the get method when I want the same results for both. Something like:

    return HttpResponse('Ha!')

def post(self. request, *args, **kwargs):
    return self.get(request, *args, **kwargs)

with dispatch left alone.

share|improve this answer

The best approach when using class-based views is not to write your own method handlers from scratch; instead, use one of the generic classes as the base and then modify just the methods which handle what you need. For example, if you have a form which needs to be submitted via POST, you should use the FormView as a base, which handles responses both GET and POST requests.

You don't need to override post() and/or get() methods themselves; instead use form_valid() to define what happens when the form is submitted:

class TestView(FormView):
    form_class = MyForm
    success_url = "/your/return/url/"
    def form_valid(self, form):
        # do something
        return redirect(self.get_success_url())
share|improve this answer
Yes thanks I am aware of that. In my case the upload is not a form, but pure JSON data (the COntent-Type is application/json), I wrote my own JSONCURDView need very much know why the django returns a blank page given dispatch returned. –  est Mar 14 '13 at 14:38
As ahsan said, that's probably because you have overriden dispatch, so your post or get methods are never called. Call super as he did to reinstate the default behaviour. –  Berislav Lopac Mar 14 '13 at 16:49
Hmm if I explicitly return in disptch() why do we need self.get() or self.post() again? The default behavior has been overriden already. –  est Mar 15 '13 at 0:41
To answer that in more detail we should dig into the Django code, but it's always better to follow the way how the application is designed instead of looking for shortcuts. In my experience, hacking directly into dispatch can lead to some unpredictable side effects; I'd recommend you to first try to exhaust all the standard approaches (in this case, using some of the many methods that handle the class-based view behaviour) and resort to low-level changes only as a last resort. –  Berislav Lopac Mar 15 '13 at 11:42
Hi, I dug into Django source and straced every system call, found out it was a nginx+uWSGi bug. See my answers below. –  est Mar 18 '13 at 8:30
up vote 0 down vote accepted

Sorry guys, this is a bug in uWSGI and nginx ...


I am really sorry for wasting your time by misleading you. And still it took me a while to figure which part of my stack went wrong.

share|improve this answer

Your Answer


