Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing attrs to be set via template on FormFieldNode #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

<input type="text" name="myfield" placeholder="Some text">
2 changes: 1 addition & 1 deletion floppyforms/templates/floppyforms/rows/li.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block field %}<li{% if classes %} class="{{ classes }}"{% endif %}>
{% block errors %}{% include "floppyforms/errors.html" with errors=field.errors %}{% endblock %}
{% block label %}{% if field|id %}<label for="{{ field|id }}">{% endif %}{{ label }}{% if label|last not in ".:!?" %}:{% endif %}{% if field|id %}</label>{% endif %}{% endblock %}
{% block widget %}{% formfield field %}{% endblock %}
{% block widget %}{% formfield field with attrs=attrs %}{% endblock %}
{% block help_text %}{% if help_text %}<span class="helptext">{{ help_text }}</span>{% endif %}{% endblock %}
{% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %}
</li>{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion floppyforms/templates/floppyforms/rows/p.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% block errors %}{% include "floppyforms/errors.html" with errors=field.errors %}{% endblock %}
<p{% if classes %} class="{{ classes }}"{% endif %}>
{% block label %}{% if field|id %}<label for="{{ field|id }}">{% endif %}{{ label }}{% if label|last not in ".:!?" %}:{% endif %}{% if field|id %}</label>{% endif %}{% endblock %}
{% block widget %}{% formfield field %}{% endblock %}
{% block widget %}{% formfield field with attrs=attrs %}{% endblock %}
{% block help_text %}{% if help_text %}<span class="helptext">{{ help_text }}</span>{% endif %}{% endblock %}
{% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %}
</p>{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion floppyforms/templates/floppyforms/rows/tr.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<th>{% block label %}{% if field|id %}<label for="{{ field|id }}">{% endif %}{{ label }}{% if label|last not in ".:!?" %}:{% endif %}{% if field|id %}</label>{% endif %}{% endblock %}</th>
<td>
{% 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 %}<br /><span class="helptext">{{ help_text }}</span>{% endif %}{% endblock %}
{% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %}
</td>
Expand Down
3 changes: 2 additions & 1 deletion floppyforms/templatetags/floppyforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +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 'using' in self.options:
try:
template_name = self.options['using'].resolve(context)
Expand All @@ -643,7 +644,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()

Expand Down
16 changes: 16 additions & 0 deletions tests/templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, '''
<p>
<label for="id_name">Name:</label>
<input type="text" name="name" required placeholder="My Name" id="id_name">
</p>
''')



class FormFieldTagTests(TestCase):
def test_valid_syntax(self):
Expand Down