django class based view returns empty string when POST

大兔子大兔子 提交于 2019-12-25 03:49:25

问题


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):
    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        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


回答1:


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

    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        # do something
        return super(TestView, self).dispatch(*args, **kwargs)

    def post(self, request, *args, **kwargs):
       # do somthing

    def get(self, request, *args, **kwargs):
       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.




回答2:


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:

def get(self, request, *args, **kwargs):
    return HttpResponse('Ha!')

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

with dispatch left alone.




回答3:


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



回答4:


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

https://stackoverflow.com/a/11115108/41948

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.



来源:https://stackoverflow.com/questions/15383367/django-class-based-view-returns-empty-string-when-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!