Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Class view didn't return an HttpResponse object. It returned None instead

Tags:

python

django

urls.py

from housepost.views import ListingPost
...
url(r'^house_post/$', ListingPost.as_view(), name='post_house'),
...

views.py

from django.http import HttpResponse
from django.contrib import messages
from django.views.generic import View
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

class ListingPost(View):

    def get(self, request, *args, **kwargs):
        messages.error(request, 'asdf', extra_tags = 'error')
        return HttpResponse('Hi')

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        super(ListingPost, self).dispatch(*args, **kwargs)

I'm returning an HttpResponse on a get request, yet I keep getting an error:

error message

The view housepost.views.ListingPost didn't return an HttpResponse object. It returned None instead.

Where am I going wrong?

like image 942
Michael Tedla Avatar asked Oct 15 '25 20:10

Michael Tedla


2 Answers

dispatch returns a HttpResponse but you don't return anything when you override it. This is the method that calls get or post and returns the response on their behalf. So the following should work:

def dispatch(self, *args, **kwargs):
    return super(ListingPost, self).dispatch(*args, **kwargs)
like image 75
JuniorCompressor Avatar answered Oct 17 '25 09:10

JuniorCompressor


Your dispatch method needs to actually return the result of calling the superclass method:

return super(ListingPost, self).dispatch(*args, **kwargs)
like image 38
Daniel Roseman Avatar answered Oct 17 '25 11:10

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!