0

In my form I have one read-only field. The content of the field will be filled by a java script and depends on the input to the editable fields. To avoid that users modify the field data, I would like to mark it as read-only.

When I mark the field as read-only in my forms.py with

'key': forms.TextInput(attrs={'disabled':'disabled'}),

it seems that the content of the field is read-only, but will not be submitted. I get the following error when I try to read form.cleaned_data['key']:

Exception Type:     MultiValueDictKeyError
Exception Value:    "Key 'key' not found in <QueryDict: ...>

Is there any better way to mark the field as 'read-only' in the Django form than 'disabled'?

Thank you for your suggestions!

neurix
  • 4,126
  • 6
  • 46
  • 71
  • It seems that 'key': forms.TextInput(attrs={'readonly':'True'}), will solve it. Sorry, should have read further in the docs. – neurix Dec 10 '11 at 07:40

1 Answers1

6

Disabled fields are never submitted in form data. But you can use, attribute readonly="readonly" , its supported in almost all browsers.

Also it would be helpful referring this article http://www.cs.tut.fi/~jkorpela/forms/readonly.html.

Happy Coding.

simplyharsh
  • 35,488
  • 12
  • 65
  • 73