errorous
errorous

Reputation: 1071

Django forms.ModelForm - placeholder not showing

I've the following code in my forms.py:

class CandidateSignUpForm(forms.ModelForm):
first_name = forms.CharField(max_length=50)
last_name = forms.CharField(max_length=50)

class Meta:
    model = User
    fields = ('__all__')
    widgets = {
        'first_name': forms.TextInput(
            attrs = {
                'placeholder':'Ime',
                'class': 'validate'
            },
        ),

        'last_name': forms.TextInput(
            attrs = {
                'placeholder': _('Prezime'),
                'class': 'validate'
            },
        ),

        'email': forms.EmailInput(
            attrs = {
                'placeholder': _('E-mail'),
                'class': 'validate'
            },
        ),

        'password': forms.PasswordInput(
            attrs = {
                'placeholder': _('Lozinka'),
                'class': 'validate'
            },
        ),

    }

Part in templates is simply {{ form.first_name }}. Now, placeholder doesn't work for first_name and last_name form fields, but does for email form field. Would someone please share some info on what am I doing wrong here?

Upvotes: 1

Views: 1198

Answers (1)

Hybrid
Hybrid

Reputation: 7049

Since you already specified the fields in the class itself, you should be using inline widgets (since the Meta widgets field is for overriding default widgets).

class CandidateSignUpForm(forms.ModelForm):
    first_name = forms.CharField(max_length=50, widget=forms.TextInput(attrs={'placeholder': 'Ime', 'class': 'validate'}))
    last_name = forms.CharField(max_length=50, widget=forms.TextInput(attrs={'placeholder': _('Prezime'), 'class': 'validate'}))

    class Meta:
        ...

https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#overriding-the-default-fields

Upvotes: 2

Related Questions