TypeError: metaclass conflict python 3; django 2
TypeError: metaclass conflict python 3; django 2
我正在学习 django 2,但遇到了问题。
我尝试继承一些 class 并出现此错误:
"TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases"
这是我的'view.py'代码:
from django.views.generic.base import TemplateView
from generic.mixins import CategoryListMixin
class MainPageView(TemplateView, CategoryListMixin):
template_name = 'mainpage.html'
但只有当 'CategoryListMixin' class 和 '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'
一切都很好。
像这样解决: 没有帮助。
可能是什么问题?
谢谢。
我找到了解决此问题的方法。我的文件和这个文件中的 class 具有相同的名称 CategoryListMixin,我需要导入 class,但我导入了文件并试图继承它。
错误在这一行:
from generic.mixins import CategoryListMixin
右行是:
from generic.mixins.CategoryListMixin import CategoryListMixin
之后一切正常
我正在学习 django 2,但遇到了问题。 我尝试继承一些 class 并出现此错误: "TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases"
这是我的'view.py'代码:
from django.views.generic.base import TemplateView
from generic.mixins import CategoryListMixin
class MainPageView(TemplateView, CategoryListMixin):
template_name = 'mainpage.html'
但只有当 'CategoryListMixin' class 和 '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'
一切都很好。
像这样解决:
我找到了解决此问题的方法。我的文件和这个文件中的 class 具有相同的名称 CategoryListMixin,我需要导入 class,但我导入了文件并试图继承它。
错误在这一行:
from generic.mixins import CategoryListMixin
右行是:
from generic.mixins.CategoryListMixin import CategoryListMixin
之后一切正常