[Fixed]-Django: Disabled Dropdown doesn't send information back to POST


Its not a django thing, its an HTML thing. Disabled form elements are not sent by the form.

[The Element] cannot receive user input nor will its value be submitted with the form.

http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1 & http://www.w3schools.com/tags/att_input_disabled.asp

you could use readonly if its on a text/textarea

something else you could do, is show the value plaintext, and submit it as a hidden field….

{{ form.field_name.label_tag }}
{{ form.field_name.value }}
<input type="hidden" name="field_name" value="{{form.field_name.value}}" />

its not very elegant, but it could get you there.

You could also take it a step further and write some JS that looks for disabled elements and adds an input with that element’s name and value after.

some sample JQuery:

//Untested, but you get the gist
        $(this).after('<input type="hidden" name="' + $(this).attr('name') + '" value="' + $(this).val() + '" />');


Well, you could set the element with hidden property in the template, using formsets in the view to build the form:


and inside the view, if the problem is the data loss, you could always set an initial value for the field that suits your model structure, since it’s a foreign key. Of course, you will have to validate the form before commiting it, and if the form is not valid, you can render it with initial values on the fields that must be always filled.


I think this is a HTML issue rather than Django, disabled form fields don’t post their values back so you’re losing the value.

Would it be possible to rebind the value to the field if validation fails? You could try something like

if form.is_valid(): # All validation rules pass
    #save form, redirect, etc.
    form.disabled_field = my_value
return render(request, 'contact.html', {'form': form,})

Obviously you’ll need to replace the field name and value with the correct data from your model.

Leave a comment