Django remove value from field with attrs disabled="disabled"

2,036 views
Skip to first unread message

eli

unread,
Sep 5, 2009, 1:52:34 PM9/5/09
to Django users
Hi,

I have problem with Django Forms and field with set attrs to
disabled="disabled"

# forms.py

class EditForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(EditForm, self).__init__(*args, **kwargs)
self.fields['title'].widget.attrs.update({'readonly':
'readonly', 'disabled': 'disabled'})

class Meta:
model = MyModel
fields = ('title', 'tagline')

# views.py

form = EditForm(request.POST, instance=my_instance)
if form.is_valid():
form.save()

# models.py

class MyModel(models.Model):
title = models.CharField(max_length=120, db_index=True)
tagline = models.TextField(, max_length=500, db_index=True)

And now, Django remove values form field with attrs 'disabled':
'disabled' ("readonly" without "disabled" works fine, but I need
disabled option in my html output). Why Django do that?

Thanks for help.

regards.

Karen Tracey

unread,
Sep 6, 2009, 12:24:15 AM9/6/09
to django...@googlegroups.com
On Sat, Sep 5, 2009 at 1:52 PM, eli <eliasz....@gmail.com> wrote:

Hi,

I have problem with Django Forms and field with set attrs to
disabled="disabled"

[snip]

And now, Django remove values form field with attrs 'disabled':
'disabled' ("readonly" without "disabled" works fine, but I need
disabled option in my html output). Why Django do that?


Django is not removing the values.  Rather the browser does not send to the server values for form elements that are disabled when the form is submitted. The HTML 4 spec here: http://www.w3.org/TR/REC-html40/interact/forms.html#disabled notes in discussing an example "Therefore, it cannot receive user input nor will its value be submitted with the form."

By setting the element to disabled you have done essentially the same thing as excluding it from the from (as far as Django can see), and you are similarly responsible for supplying values for fields that are not contained in the submitted form.  See the note here:

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form

Karen

V

unread,
Sep 6, 2009, 4:42:43 AM9/6/09
to Django users
instead of using disabled field, I wrote a "StaticField"

it's value is supplied in the general clean method, for details see
http://www.djangosnippets.org/snippets/1523/

I hope this might help.

eli

unread,
Sep 6, 2009, 7:30:07 AM9/6/09
to Django users
Thanks for help.

regards.
Reply all
Reply to author
Forward
0 new messages