Как добавить класс в поле ввода виджета Django, используя Django-Widget-Tweaks - PullRequest
0 голосов
/ 25 апреля 2019

Итак, у меня есть этот HTML-код:

<div class="custom-control custom-checkbox">
  <input class="custom-control-input">
  <label class="custom-control-label"></label>
</div>

И фильтр Django, подобный этому:

class PlayerDetailPageFilter(FilterSet):
    field_position_relationship__field_position     = filters.MultipleChoiceFilter(choices=FIELD_POSITION_CHOICES, widget=forms.CheckboxSelectMultiple)

Он использует CheckboxSelectMultiple из форм Django. Таким образом, в моем шаблоне я отображаю форму следующим образом:

{% for choice in filter_page.form.field_position_relationship__field_position %}
  <div class="custom-control custom-checkbox"> 

    {{ choice.tag }} {{ choice.choice_label }}

  </div>
{% endfor %}

Так что, используя Django-Widget-Tweaks, я подозреваю, что могу сделать это: {{ choice.tag|add_class:"custom-control-input" }}

Однако я не могу, потому что я получаю эту трассировку:

Traceback:

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  156.                 response = response.render()

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/response.py" in render
  106.             self.content = self.rendered_content

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/defaulttags.py" in render
  211.                     nodelist.append(node.render_annotated(context))

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in render
  993.             output = self.filter_expression.resolve(context)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/django/template/base.py" in resolve
  703.                 new_obj = func(obj, *arg_vals)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py" in wrapped
  12.         return fn(field, attr)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py" in add_class
  73.     return append_attr(field, 'class:' + css_class)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py" in wrapped
  12.         return fn(field, attr)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py" in append_attr
  67.     return _process_field_attributes(field, attr, process)

File "/Users/rafrasenberg/myoden/lib/python3.6/site-packages/widget_tweaks/templatetags/widget_tweaks.py" in _process_field_attributes
  26.     old_as_widget = field.as_widget

Exception Type: AttributeError at /pages/players/
Exception Value: 'SafeText' object has no attribute 'as_widget'

Может кто-нибудь объяснить, почему это? А как добавить классы в мою форму?

...