Fixed #20684 -- Added support for HTML5 boolean attributes to form widgets.
This commit is contained in:
parent
684e8a941b
commit
e61d99d96d
|
@ -2,7 +2,6 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from collections import UserList
|
from collections import UserList
|
||||||
|
@ -10,7 +9,6 @@ except ImportError: # Python 2
|
||||||
from UserList import UserList
|
from UserList import UserList
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.deprecation import RemovedInDjango18Warning
|
|
||||||
from django.utils.encoding import force_text, python_2_unicode_compatible
|
from django.utils.encoding import force_text, python_2_unicode_compatible
|
||||||
from django.utils.html import format_html, format_html_join, escape
|
from django.utils.html import format_html, format_html_join, escape
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
@ -31,19 +29,18 @@ def flatatt(attrs):
|
||||||
|
|
||||||
The result is passed through 'mark_safe'.
|
The result is passed through 'mark_safe'.
|
||||||
"""
|
"""
|
||||||
for attr_name, value in attrs.items():
|
boolean_attrs = []
|
||||||
if type(value) is bool:
|
for attr, value in list(attrs.items()):
|
||||||
warnings.warn(
|
if value is True:
|
||||||
"In Django 1.8, widget attribute %(attr_name)s=%(bool_value)s "
|
boolean_attrs.append((attr,))
|
||||||
"will %(action)s. To preserve current behavior, use the "
|
del attrs[attr]
|
||||||
"string '%(bool_value)s' instead of the boolean value." % {
|
elif value is False:
|
||||||
'attr_name': attr_name,
|
del attrs[attr]
|
||||||
'action': "be rendered as '%s'" % attr_name if value else "not be rendered",
|
|
||||||
'bool_value': value,
|
return (
|
||||||
},
|
format_html_join('', ' {0}="{1}"', sorted(attrs.items())) +
|
||||||
RemovedInDjango18Warning
|
format_html_join('', ' {0}', sorted(boolean_attrs))
|
||||||
)
|
)
|
||||||
return format_html_join('', ' {0}="{1}"', sorted(attrs.items()))
|
|
||||||
|
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
|
|
|
@ -205,6 +205,19 @@ foundation for custom widgets.
|
||||||
>>> name.render('name', 'A name')
|
>>> name.render('name', 'A name')
|
||||||
u'<input title="Your name" type="text" name="name" value="A name" size="10" />'
|
u'<input title="Your name" type="text" name="name" value="A name" size="10" />'
|
||||||
|
|
||||||
|
.. versionchanged:: 1.8
|
||||||
|
|
||||||
|
If you assign a value of ``True`` or ``False`` to an attribute,
|
||||||
|
it will be rendered as an HTML5 boolean attribute::
|
||||||
|
|
||||||
|
>>> name = forms.TextInput(attrs={'required': True})
|
||||||
|
>>> name.render('name', 'A name')
|
||||||
|
'<input name="name" type="text" value="A name" required />'
|
||||||
|
>>>
|
||||||
|
>>> name = forms.TextInput(attrs={'required': False})
|
||||||
|
>>> name.render('name', 'A name')
|
||||||
|
'<input name="name" type="text" value="A name" />'
|
||||||
|
|
||||||
.. method:: render(name, value, attrs=None)
|
.. method:: render(name, value, attrs=None)
|
||||||
|
|
||||||
Returns HTML for the widget, as a Unicode string. This method must be
|
Returns HTML for the widget, as a Unicode string. This method must be
|
||||||
|
|
|
@ -106,7 +106,8 @@ File Uploads
|
||||||
Forms
|
Forms
|
||||||
^^^^^
|
^^^^^
|
||||||
|
|
||||||
* ...
|
* Form widgets now render attributes with a value of ``True`` or ``False``
|
||||||
|
as HTML5 boolean attributes.
|
||||||
|
|
||||||
Internationalization
|
Internationalization
|
||||||
^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
@ -20,6 +20,9 @@ class FormsUtilTestCase(TestCase):
|
||||||
|
|
||||||
self.assertEqual(flatatt({'id': "header"}), ' id="header"')
|
self.assertEqual(flatatt({'id': "header"}), ' id="header"')
|
||||||
self.assertEqual(flatatt({'class': "news", 'title': "Read this"}), ' class="news" title="Read this"')
|
self.assertEqual(flatatt({'class': "news", 'title': "Read this"}), ' class="news" title="Read this"')
|
||||||
|
self.assertEqual(flatatt({'class': "news", 'title': "Read this", 'required': "required"}), ' class="news" required="required" title="Read this"')
|
||||||
|
self.assertEqual(flatatt({'class': "news", 'title': "Read this", 'required': True}), ' class="news" title="Read this" required')
|
||||||
|
self.assertEqual(flatatt({'class': "news", 'title': "Read this", 'required': False}), ' class="news" title="Read this"')
|
||||||
self.assertEqual(flatatt({}), '')
|
self.assertEqual(flatatt({}), '')
|
||||||
|
|
||||||
def test_validation_error(self):
|
def test_validation_error(self):
|
||||||
|
|
Loading…
Reference in New Issue