3

I'm learning django 2 and have a problem. I try to inherite some class and have this error: "TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases"

This is my code of 'view.py':

from django.views.generic.base import TemplateView
from generic.mixins import CategoryListMixin


class MainPageView(TemplateView, CategoryListMixin):
    template_name = 'mainpage.html'

But i've got this stuck only when 'CategoryListMixin' class is placed at another folder with 'view.py'. If I do like this:

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'

Everything is okay.

Any resolving like this: Multiple inheritance metaclass conflict didn't help. What could be a problem? Thank you.

Alexey K
  • 59
  • 5

1 Answers1

2

I found a resolving of this issue. My file and class in this file have the same name CategoryListMixin and i need to import class, but i imported file and had tried to inherite it.

Mistake was at this line:

from generic.mixins import CategoryListMixin

Right line is:

from generic.mixins.CategoryListMixin import CategoryListMixin

After that everything works correctly

Alexey K
  • 59
  • 5