Skip to content

Commit

Permalink
creating support for BoundWidget based of #122 with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
girishkumarkh committed Oct 9, 2024
1 parent 84db553 commit 5059b17
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"*test*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
21 changes: 16 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ Output:
<input id="id_name" type="text" name="name" maxlength="100" />
</div>

Fields with multiple widgets
============================

Some fields may render as a `MultiWidget`, composed of multiple subwidgets
(for example, a `ChoiceField` using `RadioSelect`). You can use the same tags
and filters, but your template code will need to include a for loop for fields
like this:

.. code-block:: html+django

{% load widget_tweaks %}

{% for widget in form.choice %}
{{ widget|add_class:"css_class_1 css_class_2" }}
{% endfor %}

Mixing render_field and filters
===============================

Expand Down Expand Up @@ -419,8 +435,3 @@ Make sure you have `tox <http://tox.testrun.org/>`_ installed, then type
tox

from the source checkout.

NOT SUPPORTED
=============

MultiWidgets: SplitDateTimeWidget, SplitHiddenDateTimeWidget
1 change: 1 addition & 0 deletions tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MyForm(Form):
with_attrs = CharField(widget=TextInput(attrs={"foo": "baz", "egg": "spam"}))
with_cls = CharField(widget=TextInput(attrs={"class": "class0"}))
date = forms.DateField(widget=SelectDateWidget(attrs={"egg": "spam"}))
choice = forms.ChoiceField(choices=[(1, "one"), (2, "two")])


def render_form(text, form=None, **context_args):
Expand Down
11 changes: 11 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,14 @@ def test_field_arroba_dot(self):
def test_field_double_colon_missing(self):
res = render_form('{{ form.simple|attr:"::class:{active:True}" }}')
assertIn(':class="{active:True}"', res)


class ChoiceFieldTest(TestCase):
def test_choice(self):
res = render_field("choice", "attr", "foo:bar")
assertIn("select", res)
assertIn('name="choice"', res)
assertIn('id="id_choice"', res)
assertIn('foo="bar"', res)
assertIn('<option value="1">one</option>', res)
assertIn('<option value="2">two</option>', res)
14 changes: 14 additions & 0 deletions widget_tweaks/templatetags/widget_tweaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ def _process_field_attributes(field, attr, process):
attribute = params[0].replace("::", ":")
value = params[1] if len(params) == 2 else True
field = copy(field)

if not hasattr(field, "as_widget"):
old_tag = field.tag

def tag(self): # pylint: disable=unused-argument
attrs = self.data["attrs"]
process(self.parent_widget, attrs, attribute, value)
html = old_tag()
self.tag = old_tag
return html

field.tag = types.MethodType(tag, field)
return field

# decorate field.as_widget method with updated attributes
old_as_widget = field.as_widget

Expand Down

0 comments on commit 5059b17

Please sign in to comment.