【问题标题】:TypeError: metaclass conflict python 3; django 2TypeError:元类冲突python 3; django 2
【发布时间】: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


    【解决方案1】:

    我找到了解决此问题的方法。我的文件和这个文件中的类同名CategoryListMixin,我需要导入类,但是我导入了文件并试图继承它。

    错误出现在这一行:

    from generic.mixins import CategoryListMixin
    

    右边是:

    from generic.mixins.CategoryListMixin import CategoryListMixin
    

    之后一切正常

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 2019-05-17
      • 2021-06-18
      • 2022-01-12
      • 2011-12-18
      • 2012-12-13
      • 2016-08-20
      • 1970-01-01
      • 2019-01-06
      相关资源
      最近更新 更多