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

Backport of 29a518006f from master
This commit is contained in:
Jon Dufresne 2017-06-02 06:46:43 -07:00 committed by Tim Graham
parent 84dac491d4
commit 1940e3daef
3 changed files with 35 additions and 3 deletions

View File

@ -10,7 +10,7 @@ from django.utils.deprecation import RemovedInDjango21Warning
from django.utils.encoding import force_text, python_2_unicode_compatible
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 ugettext_lazy as _
@ -112,7 +112,7 @@ class BoundField(object):
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,30 @@
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(CustomWidget1, self).render(name, value, attrs, renderer)
class CustomWidget2(forms.TextInput):
def render(self, *args, **kwargs):
return super(CustomWidget2, self).render(*args, **kwargs)
class CustomWidget3(forms.TextInput):
def render(self, name, value, attrs=None):
return super(CustomWidget3, self).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 "
with self.assertRaisesMessage(RemovedInDjango21Warning, msg):
str(form['baz'])