Django 1.9 adding a class to form label
<p>In my forms.py file I reconfigured the class UserForm's <code>__init__</code> function to include a css class. Unfortunately doing this only allowed me to add a class to the <code>&lt;input&gt;</code> and not the <code>&lt;label&gt;</code>. How can I add a class to the label as well?</p>

<p>Here is my forms.py file:</p>

<pre><code>class UserForm(forms.ModelForm):
    # password = forms.CharField(widget=forms.PasswordInput())
    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.fields['email'].widget.attrs = {
            'class': 'form-control'
        }

    class Meta:
        model = User
        fields = ('email',)

    def clean_email(self):
        email = self.cleaned_data['email']
        if not email:
            raise forms.ValidationError(u'Please enter an email address.')
        if User.objects.exclude(pk=self.instance.pk).filter(email=email).exists():
            raise forms.ValidationError(u'Email "%s" is already in use.' % email)
        return email
</code></pre>

<p>Thanks!</p>

<p>Look at <a href="https://github.com/kmike/django-widget-tweaks/" rel="nofollow">Django Widget Tweaks</a>.</p>

python django django-forms django-templates django-1.9