如何使用Django-Widget-Tweaks将类添加到Django小部件的输入字段

时间:2019-04-25 12:06:31

标签: django django-forms django-widget django-widget-tweaks

所以我有这段HT​​ML代码:

<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)

它使用Django表单中的CheckboxSelectMultiple。因此,在我的模板中,我渲染了这样的表单:

{% 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'

有人可以解释为什么吗?以及如何向我的表单添加类?

0 个答案:

没有答案