Django “The view didn't return an HttpResponse object.”

前端 未结 3 1641
臣服心动
臣服心动 2020-12-15 08:15

I have a simple view in which I\'m saving a form. The code seems \'clean\', but I can\'t get rid of the error:

"The view didn\'t return an HttpRespo

3条回答
  •  半阙折子戏
    2020-12-15 08:36

    If you are using the Django Rest framework. Use the below code to return the HTTP response to resolve this issue.

    from django.http import HttpResponse
    
    def TestAPI(request):
        # some logic
        return HttpResponse('Hello')
    

    JSON Response return example:

    def TestAPI(request):
        your_json = [{'key1': value, 'key2': value}]
        return HttpResponse(your_json, 'application/json')
    

    For more details about HttpResponse: https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpResponse

提交回复
热议问题