I want to customize a ClearableFileInput and need to access the checkbox_name and checkbox_id from the clearable checkbox.
If I call this field as {{ field }}
everything is rendered properly.
I can also access fields like {{ field.field.widget.clear_checkbox_label }} but I tried multiple things to get the name and the id of the clearable checkbox. I would have assumed that
{{ field.field.widget.checkbox_name }}` would work, but it does not exist. Same for id.
I already checked the ClearableFileInput class and the corresponding template.
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
checkbox_name = self.clear_checkbox_name(name)
checkbox_id = self.clear_checkbox_id(checkbox_name)
context["widget"].update(
{
"checkbox_name": checkbox_name,
"checkbox_id": checkbox_id,
"is_initial": self.is_initial(value),
"input_text": self.input_text,
"initial_text": self.initial_text,
"clear_checkbox_label": self.clear_checkbox_label,
}
)
context["widget"]["attrs"].setdefault("disabled", False)
context["widget"]["attrs"]["checked"] = self.checked
return context
{% if widget.is_initial %}{{ widget.initial_text }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}>
<label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>{% endif %}<br>
{{ widget.input_text }}:{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
and there you can see: name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"
and in my custom form template I got form
and iterate over the form.visible_fields
How can I achieve this? The only way I found was manually adding the suffixes as in ClearableFileInput: name="{{ field.html_name }}-clear" id="{{ field.html_name }}-clear_id"
Update: I found a solution now, but maybe there are better ways:
{{ field.subwidgets.0.data.checkbox_name }}
{{ field.subwidgets.0.data.checkbox_id }}