Django says “didn't return an HttpResponse object. It returned None instead.”

后端 未结 2 1849
渐次进展
渐次进展 2021-01-20 05:39

I was looking for solutions on stack, but none did helped me. The most solutions were indent-related, but I think mine is not. I\'ll appreciate it when someone can help me o

相关标签:
2条回答
  • 2021-01-20 06:17

    You have not contemplated the case where the request's verb isn't POST, in that case the function get_question will return the default value for functions without an explicit return: None. This is why is some important to always use if in conjunction with else, in functional languages such as ML and Haskell that's required and in my experience it helps you understand better the data flow of your program.

    0 讨论(0)
  • 2021-01-20 06:19

    Please check now, In get request you were not returning anything,

    def get_question(request):
        if request.method == 'POST':
            form = QuestionPostForm(request.POST)
            if form.is_valid():
                obj = QuestionPost()
                obj.question = form.cleaned_data['question']
                obj.tag = form.cleaned_data['tag']
                obj.save()
                return HttpResponseRedirect('forum/index.html',{'form':form})
    
        else:
            form = QuestionPostForm()
        return render_to_response(request, 'forum/index.html', {'form': form})
    
    0 讨论(0)
提交回复
热议问题