Access form field attributes in templated Django

后端 未结 2 1163
既然无缘
既然无缘 2021-02-14 12:12

I\'ve been doing some custom forms with django but I don\'t get how to access attributes that a specific form field has attached via the forms.py.

def putErrorIn         


        
2条回答
  •  暖寄归人
    2021-02-14 12:53

    In other cases it can can be useful to set and get field attributes.

    Setting in form's init function:

    self.fields['some_field'].widget.attrs['readonly'] = True
    

    ... and accessing it in a template:

    {{ form.some_field.field.widget.attrs.readonly }}
    

提交回复
热议问题