jazzblue jazzblue - 1 year ago 128
Python Question

Django ModelForm label customization

The Django documentation explains how to use labels customization in a ModelForm model map here:
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download