From e61d99d96daedfea2f6ba071945ef0d2f86883c7 Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Sun, 23 Mar 2014 00:16:49 +0700 Subject: [PATCH] Fixed #20684 -- Added support for HTML5 boolean attributes to form widgets. --- django/forms/utils.py | 27 ++++++++++++--------------- docs/ref/forms/widgets.txt | 13 +++++++++++++ docs/releases/1.8.txt | 3 ++- tests/forms_tests/tests/test_util.py | 3 +++ 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/django/forms/utils.py b/django/forms/utils.py index 2147014a5d..f7f53e9a0a 100644 --- a/django/forms/utils.py +++ b/django/forms/utils.py @@ -2,7 +2,6 @@ from __future__ import unicode_literals import json import sys -import warnings try: from collections import UserList @@ -10,7 +9,6 @@ except ImportError: # Python 2 from UserList import UserList 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.html import format_html, format_html_join, escape from django.utils import timezone @@ -31,19 +29,18 @@ def flatatt(attrs): The result is passed through 'mark_safe'. """ - for attr_name, value in attrs.items(): - if type(value) is bool: - warnings.warn( - "In Django 1.8, widget attribute %(attr_name)s=%(bool_value)s " - "will %(action)s. To preserve current behavior, use the " - "string '%(bool_value)s' instead of the boolean value." % { - 'attr_name': attr_name, - 'action': "be rendered as '%s'" % attr_name if value else "not be rendered", - 'bool_value': value, - }, - RemovedInDjango18Warning - ) - return format_html_join('', ' {0}="{1}"', sorted(attrs.items())) + boolean_attrs = [] + for attr, value in list(attrs.items()): + if value is True: + boolean_attrs.append((attr,)) + del attrs[attr] + elif value is False: + del attrs[attr] + + return ( + format_html_join('', ' {0}="{1}"', sorted(attrs.items())) + + format_html_join('', ' {0}', sorted(boolean_attrs)) + ) @python_2_unicode_compatible diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt index 5bd0a0b5ea..f6b8fd6c47 100644 --- a/docs/ref/forms/widgets.txt +++ b/docs/ref/forms/widgets.txt @@ -205,6 +205,19 @@ foundation for custom widgets. >>> name.render('name', 'A name') u'' + .. 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') + '' + >>> + >>> name = forms.TextInput(attrs={'required': False}) + >>> name.render('name', 'A name') + '' + .. method:: render(name, value, attrs=None) Returns HTML for the widget, as a Unicode string. This method must be diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 96d942ecbc..2214b77839 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -106,7 +106,8 @@ File Uploads Forms ^^^^^ -* ... +* Form widgets now render attributes with a value of ``True`` or ``False`` + as HTML5 boolean attributes. Internationalization ^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/forms_tests/tests/test_util.py b/tests/forms_tests/tests/test_util.py index 5f0b549bdb..d271d784c9 100644 --- a/tests/forms_tests/tests/test_util.py +++ b/tests/forms_tests/tests/test_util.py @@ -20,6 +20,9 @@ class FormsUtilTestCase(TestCase): 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", '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({}), '') def test_validation_error(self):