Tuesday, October 28, 2014

Dictionary passed to widget attr gets mutated

When a dictionary is passed to a Widget as the `attr` kwarg, to be used in filling HTML attributes, this dictionary is mutated, so cannot be reused. See below example to see a clear problem with this. Note how in the first form, when printer as_p(), the second field (session_end) gets an incorrect 'type' HTML attribute. This was tested in django 1.6

from django import forms    class FormWithBug(forms.Form):      attrs = {'type':'time', 'required':'true'}      session_start = forms.TimeField(widget=forms.TimeInput(attrs= attrs))      session_end = forms.TimeField(widget=forms.TimeInput(attrs=attrs))    class FormWithoutBug(forms.Form):      session_start = forms.TimeField(widget=forms.TimeInput(attrs= {'type':'time', 'required':'true'}))      session_end = forms.TimeField(widget=forms.TimeInput(attrs={'type':'time', 'required':'true'}))    FormWithBug().as_p()  FormWithoutBug().as_p()

In [36]: FormWithBug().as_p()  Out[36]: u'<p><label for="id_session_start">Session start:</label> <input id="id_session_start" name="session_start" required="true" type="time" /></p>\n<p><label for="id_session_end">Session end:</label> <input id="id_session_end" name="session_end" required="true" type="text" /></p>'    In [37]: FormWithoutBug().as_p()  Out[37]: u'<p><label for="id_session_start">Session start:</label> <input id="id_session_start" name="session_start" required="true" type="time" /></p>\n<p><label for="id_session_end">Session end:</label> <input id="id_session_end" name="session_end" required="true" type="time" /></p>'

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bcffb3b1-4fbb-4fd6-8ab0-ee1080fc42eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment