django-debug-toolbar with django-cms and Django 1.

2019-08-01 05:33发布

问题:

I keep hitting an error when trying to use django-debug-toolbar and django-cms together.

"MpttMeta has no attribute 'class'"

I have a feeling it's something to do with the Mptt app bundled with Django CMS, but I'm not sure, and I've seen this on a few projects but I'm surprised I can't find a direct hit for the error message in Google so I thought I'd post here.

I've tried using latest released version of debug toolbar, also the develop branch, and also dcramer's fork, but it's making no difference. I'm on Django 1.3 and Django CMS 2.1.3.

Any ideas?

Thanks!

回答1:

It could be any problem related to Django 1.3.

Django CMS 2.1.3 supports only 1.2.X branch: http://docs.django-cms.org/en/2.1.3/getting_started/installation.html#requirements

Jonas Obrist, Django CMS dev says "Maybe a minor version of 2.1 will add official 1.3 support"



回答2:

The problem is that django-debug-toolbar expects the MpttMeta class needs to be a 'new style' class, which is a fairly straightforward patch (line 33 in django-cms/publisher/mptt_support.py). Change:

class MpttMeta:

to

class MpttMeta(object):

In Django-CMS 2.1.3, they still have their own monkey-patched mptt bundled with Django-CMS. In the next release of Django-CMS will no longer bundle its own mptt and will instead rely on the independently developed package.



回答3:

Or you can put this in yours .... urls.py for example. Not in settings.py because project will not start.

from publisher.mptt_support import MpttMeta
if not hasattr(MpttMeta, '__class__'):
    MpttMeta.__class__ = type


回答4:

Caught AttributeError while rendering: class MpttMeta has no attribute '__class__'

I believe this had to do with the way the MPTTMeta class is loaded into the metaclass (MPTTModelBase) making it not have a class attribute.

A monkeypatch fix is to wrap the offending statement in django-debug-toolbar like so:

try:
    text = "method %s on %s object" % (receiver.__name__, receiver.im_self.__class__.__name__)
except:
    text = "method %s on %s object" % (receiver.__name__, type(receiver.im_self).__name__)

This changes the output slightly to become

method finish_mptt_class on classobj object

Clearly not a permanent fix, but it gets you the debug-toolbar + django-cms working.