Why do I get NameError: name '_' is not defined when setting custom templates for djangocms-video?

Viewed 338

I am trying to get custom templates working for djangocms-video.

So far there is a fresh djangocms project set up with some bootstrap and running fine.

According to the readme we would need to specify this in the settings.py to make a custom template available (in this case a template named "feature"):

DJANGOCMS_VIDEO_TEMPLATES = [
    ('feature', _('Featured Version')),
]

After setting this and running manage.py this error comes up:

    ('feature', _('Featured Version')),
NameError: name '_' is not defined

According to other threads we would need to import gettext like this in the modely.py: from django.utils.translation import gettext as _ or like this: django.utils.translation import ugettext_lazy as _

No luck so far. What am I missing here?

Here is some info on the environment:

python --version
Python 3.9.2
pip list
Package                    Version
-------------------------- -----------
asgiref                    3.4.1
cssselect2                 0.4.1
dj-database-url            0.5.0
Django                     3.1.14
django-classy-tags         2.0.0
django-cms                 3.8.0
django-filer               2.1.2
django-formtools           2.3
django-js-asset            1.2.2
django-mptt                0.13.4
django-polymorphic         3.0.0
django-sekizai             2.0.0
django-treebeard           4.5.1
djangocms-admin-style      2.0.2
djangocms-attributes-field 2.0.0
djangocms-bootstrap4       2.0.0
djangocms-file             3.0.0
djangocms-googlemap        2.0.0
djangocms-icon             2.0.0
djangocms-installer        2.0.0
djangocms-link             3.0.0
djangocms-picture          3.0.0
djangocms-style            3.0.0
djangocms-text-ckeditor    4.0.0
djangocms-video            3.0.0
easy-thumbnails            2.8
html5lib                   1.1
lxml                       4.7.1
Pillow                     9.0.0
pip                        21.3.1
pkg_resources              0.0.0
pytz                       2021.3
pytz-deprecation-shim      0.1.0.post0
reportlab                  3.6.5
setuptools                 44.1.1
six                        1.16.0
sqlparse                   0.4.2
svglib                     1.1.0
tinycss2                   1.1.1
tzdata                     2021.5
tzlocal                    4.1
Unidecode                  1.1.2
webencodings               0.5.1
1 Answers

In Django, the gettext_lazy(…) function [Django-doc] is often imported as _ to manage translations. This is explained in the Standard translation:

Python’s standard library gettext module installs _() into the global namespace, as an alias for gettext(). In Django, we have chosen not to follow this practice, for a couple of reasons

(…)

Because of how xgettext (used by makemessages) works, only functions that take a single string argument can be imported as _:

You thus should add:

from django.utils.translation import gettext_lazy as _

at the top of the file.

If you thus define something like _('Featured Version'), you can run makemessages to generate translation files and fill in the translation per language.

Related