fer0m
fer0m

Reputation: 244

Django ModelForm. Hide models name

I have a ModelForm:

class First_Form(forms.ModelForm):
  class Meta:
    model = Post
    fields = ('text',)
    widgets = {
        'text': forms.Textarea(attrs={"class": "form-control",
                                      "id": "exampleFormControlInput1",
                                      "placeholder": "Enter your YouTube link",
                                      "rows": 1, }), }

On my site it looks like:

Site Ilustration

Can I hide name of ModelForm field? - "Text"? I want to show only InputField without "Text:"

Thank you!

Upvotes: 0

Views: 314

Answers (2)

ruddra
ruddra

Reputation: 51948

You can do that by two ways, one change verbose name in models:

class Post(models.Model):
   text = models.CharField(verbose_name="Not text", max_length=255)

Or override the First_form to add verbose name:

class First_Form(forms.ModelForm):
  class Meta:
    model = Post
    fields = ('text',)
    widgets = {
        'text': forms.Textarea(attrs={"class": "form-control",
                                      "id": "exampleFormControlInput1",
                                      "placeholder": "Enter your YouTube link",
                                      "rows": 1, }), }
    labels = {
        'text': 'Not text',
    }

More information can be found in this documentation.

Upvotes: 2

Charnel
Charnel

Reputation: 4432

You can try adding this:

labels = {
    'text': '',
}

or this:

class First_Form(forms.ModelForm):
...
    def __init__(self, *args, **kwargs): 
        super(ModelForm, self).__init__(*args, **kwargs)
        self.fields['text'].label = ''

Upvotes: 2

Related Questions