1

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?

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.