Django-cms: Advanced page settings not working

Created on 4 Jun 2019  路  8Comments  路  Source: django-cms/django-cms

Advanced page settings raises TypeError: __str__ returned non-string (type __proxy__) exception.

Constant "Inherit the template of the nearest ancestor" (cms.utils.conf) is wrapped in ugettext_lazy 2 times. First in cms.utils.conf, then in cms.models.pagemodel (template_choices attribute). This raises exception when casting to str:

from cms.models import Page
str(Page.template_choices[-1][1])

Environment

  • Python version: 3.6.5
  • Django version: 2.1.8
  • django CMS version: 3.6.0

Most helpful comment

Until this gets merged, I found the following monkey patch to work for those that can't wait and need this to be fixed beforehand, or either can't or don't want to fork/modify CMS's core codebase:

from cms.utils import conf

def get_templates():
    templates = conf.get_templates()
    return [(x, str(y)) for x, y in templates]


conf.COMPLEX['TEMPLATES'] = get_templates

Drop the above into anything that gets imported early on in the import sequence (an apps.py file works), and the issue is resolved.

All 8 comments

I'm getting this as well & can't see a way to resolve it at the moment.

The Page model attr which causes this is on line 170;

template_choices = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')]

Environment

  • Python version: 3.6
  • django version: 2.1.11
  • django CMS version: 3.6.0

I'm currently using django cms with ugettext removed on line

# utils/conf.py
templates.append((constants.TEMPLATE_INHERITANCE_MAGIC, _('Inherit the template of the nearest ancestor')))

Fixed code:

ugettext_noop('Inherit the template of the nearest ancestor')
templates.append((constants.TEMPLATE_INHERITANCE_MAGIC, 'Inherit the template of the nearest ancestor'))

Ugly, but it works.

Per a suggestion here https://stackoverflow.com/questions/51959209/django-error-str-returned-non-string-type-proxy

I tried the fix in django-cms/cms/utils/conf.py and it seems to work.

from django.utils.translation import ugettext_lazy as _

becomes

from django.utils.translation import ugettext as _

I'm not sure of the broader implications of this but the monkeypatch is working for me,

Django 2.1
Django CMS 3.6.0
Python 3.6.8

For what it's worth, this issue seems to only be triggered if settings.USE_I18N is False.

Not only in django-cms/cms/utils/conf.py, but in:
django-cms/cms/models/pagemodel.py too.
from django.utils.translation import ugettext_lazy as _

becomes

from django.utils.translation import ugettext as _

And it works for me.

i am also facing the same issue. Any expected date for a fix ?

This is still an issue with Django CMS 3.7.4, Django 2.2.17 and Python 3.6.8.
And I confirm that the changes proposed by @explody and @bwrm fix the problem.

Until this gets merged, I found the following monkey patch to work for those that can't wait and need this to be fixed beforehand, or either can't or don't want to fork/modify CMS's core codebase:

from cms.utils import conf

def get_templates():
    templates = conf.get_templates()
    return [(x, str(y)) for x, y in templates]


conf.COMPLEX['TEMPLATES'] = get_templates

Drop the above into anything that gets imported early on in the import sequence (an apps.py file works), and the issue is resolved.

Was this page helpful?
0 / 5 - 0 ratings