Access form field attributes in templated Django

后端 未结 2 1156
既然无缘
既然无缘 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 13:02

    It looks like you just want to display form errors for each field. After the form is cleaned or validated in the view, the fields should contain the error messages. So that you can display them in the template like so:

    ...
    {{ form.field_1.errors|join:", " }} {{ form.field_1.label_tag }} {{ form.field_1 }}
    ...

    If however you really want to display the form field attributes then you can try something like:

    {{ form.field_1.field.widget.attrs.maxlength }}
    

提交回复
热议问题