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

Add support for dynamic setting of attributes from context based on #151 #152

Open
wants to merge 3 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
36 changes: 36 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,42 @@ You can be creative with these variables: e.g. a context processor could
set a default CSS error class on all fields rendered by
``{% render_field %}``.

In addition, it is possible to dynamically set attributes using a variable of type dict from context
and setting it equal to ``attr_dict``. The content of ``attr_dict`` its order around explicitly named
attributes matters; it is possible to set a default attribute that will be overwritten by ``attr_dict``
by positioning it to the left, or conversely to have an attribute always overwrite one in ``attr_dict``
by positioning it to the right.

Examples:

Let context_dict_var be:

.. code-block:: html+django

context_dict_var = {"type":"text", id="my_username_id", placeholder="Login"}

The following line:
.. code-block:: html+django

{% render_field form.field attr_dict=context_dict %}

returns:

.. code-block:: html+django

<input type="text" id="my_field_id" placeholder="Input here" />

The following line:
.. code-block:: html+django

{% render_field form.field id="default_id" label="default_label" attr_dict=context_dict placeholder="Overwrite" %}

returns:

.. code-block:: html+django

<input type="text" id="default_id" label="default_label" placeholder="Overwrite" />

attr
----
Adds or replaces any single html attribute for the form field.
Expand Down
20 changes: 17 additions & 3 deletions widget_tweaks/templatetags/widget_tweaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,25 @@ def render(self, context):
bounded_field = append_attr(
bounded_field, f"class:{context['WIDGET_REQUIRED_CLASS']}"
)
attr_dict = {}
for k, v in self.set_attrs:
if k == "type":
bounded_field.field.widget.input_type = v.resolve(context)
if k == "attr_dict":
resolved_dict = v.resolve(context)
if not isinstance(resolved_dict, dict):
raise ValueError(f"{k} must be of type dict.")
attr_dict.update(resolved_dict)
else:
bounded_field = set_attr(bounded_field, f"{k}:{v.resolve(context)}")
attr_dict[k] = v.resolve(context)
for k, v in attr_dict.items():
if v:
if isinstance(v, bool):
bounded_field = set_attr(bounded_field, f"{k}")
if k == "type":
bounded_field.field.widget.input_type = v
else:
bounded_field = set_attr(bounded_field, f"{k}:{v}")
else:
bounded_field = remove_attr(bounded_field, k)
for k, v in self.append_attrs:
bounded_field = append_attr(bounded_field, f"{k}:{v.resolve(context)}")
return str(bounded_field)
Expand Down