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"),
}
Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.
Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.
{{ form.as_p }} – Render Django Forms as paragraph. {{ form.as_ul }} – Render Django Forms as list.
If somebody else are looking for it at this time (2016 - Django 1.9)
It works just like this for me:
labels = {
"title": "Rule Title",
"other_field": "Other Title"
}
With out the lazy import.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With