Michael Tedla
Michael Tedla

Reputation: 1169

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

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?

Upvotes: 4

Views: 2721

Answers (2)

JuniorCompressor
JuniorCompressor

Reputation: 20025

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)

Upvotes: 4

Daniel Roseman
Daniel Roseman

Reputation: 599946

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

return super(ListingPost, self).dispatch(*args, **kwargs)

Upvotes: 1

Related Questions