Django - Mixin与TemplateView

时间:2014-05-19 13:09:21

标签: django mixins django-class-based-views django-generic-views class-based-views

我正在尝试执行以下操作,但我一直收到错误'RegionsView' object has no attribute 'method'。我究竟做错了什么?感谢

#views.py

class _LanguageMixin(object):

    def dispatch(self, request, *args, **kwargs):
        self.langcode = kwargs.pop("langcode")
        self.language = get_object_or_404(Language, pk=self.langcode)
        return super(_LanguageMixin, self).dispatch(self, request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(_LanguageMixin, self).get_context_data(self, **kwargs)
        context.update({"language": self.language,
                        "languages": Language.objects.values_list('code',
                                                                  flat=True)})
        return context


class RegionsView(_LanguageMixin, TemplateView):
    template_name = "regions.html"

    def get_context_data(self, **kwargs):
        context = super(RegionsView, self).get_context_data(self, **kwargs)

        regions = #......

        context.update({"regions": regions})
        return context


#urls.py

url(r'^(?P<langcode>[a-zA-Z-]+)/regions/$', RegionsView.as_view(), name='regions')

1 个答案:

答案 0 :(得分:2)

return super(_LanguageMixin, self).dispatch(request, *args, **kwargs)

而不是

return super(_LanguageMixin, self).dispatch(self, request, *args, **kwargs)

request.method函数中使用dispatch,但您使用self对象