From 1125b11fb72e88d1a77002717504aadb16bfff40 Mon Sep 17 00:00:00 2001 From: Greg Hinch Date: Mon, 17 Nov 2014 12:16:45 +0000 Subject: [PATCH 1/3] allowing attrs to be set via template on FormFieldNode --- floppyforms/templates/floppyforms/rows/li.html | 2 +- floppyforms/templates/floppyforms/rows/p.html | 2 +- floppyforms/templates/floppyforms/rows/tr.html | 2 +- floppyforms/templatetags/floppyforms.py | 3 ++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/floppyforms/templates/floppyforms/rows/li.html b/floppyforms/templates/floppyforms/rows/li.html index 42ce4b3..6327c00 100644 --- a/floppyforms/templates/floppyforms/rows/li.html +++ b/floppyforms/templates/floppyforms/rows/li.html @@ -3,7 +3,7 @@ {% block field %} {% block errors %}{% include "floppyforms/errors.html" with errors=field.errors %}{% endblock %} {% block label %}{% if field|id %}{% endif %}{% endblock %} - {% block widget %}{% formfield field %}{% endblock %} + {% block widget %}{% formfield field with attrs=attrs %}{% endblock %} {% block help_text %}{% if help_text %}{{ help_text }}{% endif %}{% endblock %} {% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %} {% endblock %} diff --git a/floppyforms/templates/floppyforms/rows/p.html b/floppyforms/templates/floppyforms/rows/p.html index 62f2dfe..20bade3 100644 --- a/floppyforms/templates/floppyforms/rows/p.html +++ b/floppyforms/templates/floppyforms/rows/p.html @@ -4,7 +4,7 @@ {% block errors %}{% include "floppyforms/errors.html" with errors=field.errors %}{% endblock %} {% block label %}{% if field|id %}{% endif %}{% endblock %} - {% block widget %}{% formfield field %}{% endblock %} + {% block widget %}{% formfield field with attrs=attrs %}{% endblock %} {% block help_text %}{% if help_text %}{{ help_text }}{% endif %}{% endblock %} {% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %}

{% endblock %} diff --git a/floppyforms/templates/floppyforms/rows/tr.html b/floppyforms/templates/floppyforms/rows/tr.html index dd01db6..8f8b82a 100644 --- a/floppyforms/templates/floppyforms/rows/tr.html +++ b/floppyforms/templates/floppyforms/rows/tr.html @@ -4,7 +4,7 @@ {% block label %}{% if field|id %}{% endif %}{% endblock %} {% block errors %}{% include "floppyforms/errors.html" with errors=field.errors %}{% endblock %} - {% block widget %}{% formfield field %}{% endblock %} + {% block widget %}{% formfield field with attrs=attrs %}{% endblock %} {% block help_text %}{% if help_text %}
{{ help_text }}{% endif %}{% endblock %} {% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %} diff --git a/floppyforms/templatetags/floppyforms.py b/floppyforms/templatetags/floppyforms.py index 88dc484..be2a538 100644 --- a/floppyforms/templatetags/floppyforms.py +++ b/floppyforms/templatetags/floppyforms.py @@ -610,6 +610,7 @@ def render(self, context): extra_context = self.get_extra_context(context) template_name = config.retrieve('widget_template', bound_field=bound_field) + attrs = extra_context.pop('attrs') if 'attrs' in extra_context else {} if 'using' in self.options: try: template_name = self.options['using'].resolve(context) @@ -630,7 +631,7 @@ def render(self, context): # template name and context instance parameters with attributes(widget, template_name=template_name, context_instance=context_instance) as widget: - output = bound_field.as_widget(widget=widget) + output = bound_field.as_widget(widget=widget, attrs=attrs) config.pop() From 56469a1e4c783f7c9c5e11d3c9f3b8c8245bbfb7 Mon Sep 17 00:00:00 2001 From: Greg Hinch Date: Tue, 20 Jan 2015 11:57:45 +0000 Subject: [PATCH 2/3] adding documentation about passing attrs dictionary via formrow tag --- docs/customization.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/customization.rst b/docs/customization.rst index cfa2edd..f9591c5 100644 --- a/docs/customization.rst +++ b/docs/customization.rst @@ -108,3 +108,24 @@ If you want only the attribute's key to be rendered, set it to ``True``: return ctx This will simply add ``awesome`` as a key-only attribute. + +You can also pass an ``attrs`` dictionary to the ``{% formrow %}`` template tag, +which will then subsequently be passed to the rendered widget. + +Pass this to your context + +.. code-block:: python + + myfield_attrs = dict(placeholder="Some text") + +And use like this in your HTML + +.. code-block:: jinja + + {% formrow form.myfield with attrs=myfield_attrs %} + +Which will output something like + +.. code-block:: jinja + + From 84d8ef4c9b988fa38ac088685ee4f1665de43f3d Mon Sep 17 00:00:00 2001 From: Greg Hinch Date: Tue, 20 Jan 2015 15:29:30 +0000 Subject: [PATCH 3/3] adding test for setting attrs via template --- floppyforms/templatetags/floppyforms.py | 2 +- tests/templatetags.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/floppyforms/templatetags/floppyforms.py b/floppyforms/templatetags/floppyforms.py index 2d50b7f..1f444d9 100644 --- a/floppyforms/templatetags/floppyforms.py +++ b/floppyforms/templatetags/floppyforms.py @@ -623,7 +623,7 @@ def render(self, context): extra_context = self.get_extra_context(context) template_name = config.retrieve('widget_template', bound_field=bound_field) - attrs = extra_context.pop('attrs') if 'attrs' in extra_context else {} + attrs = extra_context.pop('attrs', {}) if 'using' in self.options: try: template_name = self.options['using'].resolve(context) diff --git a/tests/templatetags.py b/tests/templatetags.py index 59cd2c3..98c0c57 100644 --- a/tests/templatetags.py +++ b/tests/templatetags.py @@ -668,6 +668,22 @@ def test_formconfig_gets_popped_after_formrow_tag(self): Extra argument: first argument ''') + def test_attrs_are_passed_to_widget(self): + + form = SimpleForm() + name_attrs = {'placeholder': 'My Name'} + rendered = render('''{% form form using %} + {% formrow form.name with attrs=name_attrs %} + {% endform %}''', {'form': form, 'name_attrs': name_attrs}) + + self.assertHTMLEqual(rendered, ''' +

+ + +

+ ''') + + class FormFieldTagTests(TestCase): def test_valid_syntax(self):