Add class to Django label_tag() output

前端 未结 8 1175
独厮守ぢ
独厮守ぢ 2020-12-01 02:53

I need some way to add a class attribute to the output of the label_tag() method for a forms field.

I see that there is the ability to pass in an attrs dictionary a

8条回答
  •  -上瘾入骨i
    2020-12-01 03:39

    How about adding the CSS class to the form field in the forms.py, like:

    class MyForm(forms.Form):
        title = forms.CharField(widget=forms.TextInput(attrs={'class': 'foo'}))
    

    then I just do the following in the template:

    
    

    Of course this can easily be modified to work within a for loop tag in the template.

提交回复
热议问题