Django Class-based mixin view does not return a HttpResponse object
尝试使用mixin在django中创建基于类的视图,但始终收到以下错误消息:
1 | ValueError: The view twitter.views.TwitterExampleView didn't return an HttpResponse object. |
如下面的代码所示,我从视图扩展到为应用创建一个基本的twitter view来处理从twitter返回的错误消息。该视图进一步扩展到twitter network view,其中twitter网络连接到该视图。其他类允许对传递到请求中的参数进行一些转换。最后一个类,twitterexampleview,包括用于参数转换的mixin和twitternetworkview。它的get方法将字符串"blah"作为占位符返回。只有当它冒泡回到分派中时,才会给出响应,所以我忽略了什么?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class TwitterView(View): def dispatch(self, request, *args, **kwargs): try: response = super(TwitterView, self).dispatch(request, *args, **kwargs) return render_response(request, response) except TwitterApiException, e: return return_error(request, e, e.status_code) class TwitterNetworkView(TwitterView): def dispatch(self, request, *args, **kwargs): self.network = get_network_or_404(request.user, kwargs['network_id']) super(TwitterNetworkView, self).dispatch(request, *args, **kwargs) class DefineParamsMixin(object): def get_params(self): return null class TwitterPagedDefineParams(DefineParamsMixin): def get_params(self): return define_params( Param('page'), Param('since'), Param('before'), Param('limit', transform_func=int) ) class TwitterExampleView(TwitterPagedDefineParams, TwitterNetworkView): def get(self, request, *args, **kwargs): return"blahhhhh" |
号
你的
您需要返回一个httpResponse对象。
1 2 3 4 5 6 7 8 | from django.http import HttpResponse ... class TwitterExampleView(TwitterPagedDefineParams, TwitterNetworkView): def get(self, request, *args, **kwargs): return HttpResponse("blahhhhh") |
查看https://docs.djangoproject.com/en/1.7/intro/tutorial03/编写第一个视图