0

I need something like this:

<div>
<input type="text"></input>
<input type="hidden" name="attr1" value="true">
<input type="hidden" name="attr2" value="true">
<input type="hidden" name="attr3" value="false">
</div>
<div>
<input type="text"></input>
<input type="hidden" name="attr11" value="true">
<input type="hidden" name="attr22" value="true">
<input type="hidden" name="attr33" value="true">
</div>

I need to attach some extra non-standard attributes to each field. What is the best way to do this? How to realize above example? I can do this with custom template in django but it is unnecessary client-server communication.

Cœur
  • 37,241
  • 25
  • 195
  • 267
milandjukic88
  • 1,065
  • 3
  • 13
  • 30

1 Answers1

2

You can set any attributes you want in the form. For example you can set rows and cols attributes like this:

class MyForm(forms.Form):
    description = forms.Textarea(attrs={'my_attr': 'and_value'})

The rendered result is:

<textarea my_attr="and_value" name="description" id="id_description"></textarea>
niekas
  • 8,187
  • 7
  • 40
  • 58