Fixed #28265 -- Prevented renderer warning on Widget.render() with **kwargs.

This commit is contained in:
Jon Dufresne 2017-06-02 06:46:43 -07:00 committed by Tim Graham
parent 00093daec9
commit 29a518006f
3 changed files with 40 additions and 3 deletions

View File

@ -6,7 +6,7 @@ from django.forms.widgets import Textarea, TextInput
from django.utils.deprecation import RemovedInDjango21Warning
from django.utils.functional import cached_property
from django.utils.html import conditional_escape, format_html, html_safe
from django.utils.inspect import func_supports_parameter
from django.utils.inspect import func_accepts_kwargs, func_supports_parameter
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
@ -103,7 +103,7 @@ class BoundField:
name = self.html_initial_name
kwargs = {}
if func_supports_parameter(widget.render, 'renderer'):
if func_supports_parameter(widget.render, 'renderer') or func_accepts_kwargs(widget.render):
kwargs['renderer'] = self.form.renderer
else:
warnings.warn(

View File

@ -9,4 +9,6 @@ Django 1.11.3 fixes several bugs in 1.11.2.
Bugfixes
========
* ...
* Removed an incorrect deprecation warning about a missing ``renderer``
argument if a ``Widget.render()`` method accepts ``**kwargs``
(:ticket:`28265`).

View File

@ -0,0 +1,35 @@
from django import forms
from django.test import SimpleTestCase
from django.utils.deprecation import RemovedInDjango21Warning
class RenderDeprecationTests(SimpleTestCase):
def test_custom_widget_renderer_warning(self):
class CustomWidget1(forms.TextInput):
def render(self, name, value, attrs=None, renderer=None):
return super().render(name, value, attrs, renderer)
class CustomWidget2(forms.TextInput):
def render(self, *args, **kwargs):
return super().render(*args, **kwargs)
class CustomWidget3(forms.TextInput):
def render(self, name, value, attrs=None):
return super().render(name, value, attrs)
class MyForm(forms.Form):
foo = forms.CharField(widget=CustomWidget1)
bar = forms.CharField(widget=CustomWidget2)
baz = forms.CharField(widget=CustomWidget3)
form = MyForm()
str(form['foo']) # No warning.
str(form['bar']) # No warning.
msg = (
"Add the `renderer` argument to the render() method of <class "
"'forms_tests.widget_tests.test_render_deprecation"
".RenderDeprecationTests.test_custom_widget_renderer_warning.<locals>"
".CustomWidget3'>. It will be mandatory in Django 2.1."
)
with self.assertRaisesMessage(RemovedInDjango21Warning, msg):
str(form['baz'])