neurix
neurix

Reputation: 4316

Django: read-only field in form not submitted

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!

Upvotes: 0

Views: 4014

Answers (1)

simplyharsh
simplyharsh

Reputation: 36373

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.

Upvotes: 6

Related Questions