【发布时间】:2018-09-23 21:58:41
【问题描述】:
我正在学习 django 2,但遇到了问题。 我尝试继承某个类并出现此错误: “TypeError:元类冲突:派生类的元类必须是其所有基类的元类的(非严格)子类”
这是我的“view.py”代码:
from django.views.generic.base import TemplateView
from generic.mixins import CategoryListMixin
class MainPageView(TemplateView, CategoryListMixin):
template_name = 'mainpage.html'
但是只有当“CategoryListMixin”类被放置在另一个带有“view.py”的文件夹中时,我才会遇到这个问题。如果我这样做:
from django.shortcuts import render
from django.views.generic.base import TemplateView
from django.views.generic.base import ContextMixin
class CategoryListMixin(ContextMixin):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['current_url'] = self.request.path
return context
class MainPageView(TemplateView, CategoryListMixin):
template_name = 'mainpage.html'
一切正常。
任何像这样的解决方法:Multiple inheritance metaclass conflict 没有帮助。 可能是什么问题? 谢谢。
【问题讨论】:
标签: python-3.x web django-2.0