Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ModelForm label customization

Tags:

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"),
        }
like image 877
jazzblue Avatar asked Jan 08 '14 04:01

jazzblue


People also ask

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

What is ModelForm in Django?

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.

What is form AS_P in Django?

{{ form.as_p }} – Render Django Forms as paragraph. {{ form.as_ul }} – Render Django Forms as list.


1 Answers

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.

like image 91
QUHO Avatar answered Oct 18 '22 14:10

QUHO