jazzblue jazzblue - 5 months ago 17
Python Question

Django ModelForm label customization

The Django documentation explains how to use labels customization in a ModelForm model map here:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/.
However, when I try to follow it I get the error:

name '_' is not defined.


What am I doing wrong?

Also, why there should be a
_
symbol before the parenthesis?

Here is my code:

class RuleForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ModelForm, self).__init__(*args, **kwargs)
self.css_class = "rule"

class Meta:
model = Rule
fields = ("user", "title")
exclude = ("user")
widgets = {
"title" : TextInput(attrs={"class" : "title"}),
}
labels = {
"title": _("Rule Title"),
}

Answer

Did you import following?

from django.utils.translation import ugettext_lazy as _

ugettext, ugettext_lazy are used to specify translation strings. _ is used to save typings.

See Translation | Django documentation.

Comments