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)
我在get请求上返回HttpResponse,但我不断收到错误消息:
错误信息
视图housepost.views.ListingPost没有返回HttpResponse对象。它返回None。
我要去哪里错了?
python大神给出的解决方案
dispatch
返回HttpResponse
,但是覆盖它时不返回任何内容。这是调用get
或post
并代表它们返回响应的方法。因此,以下应该工作:
def dispatch(self, *args, **kwargs):
return super(ListingPost, self).dispatch(*args, **kwargs)