开发者

How do I add different content for a checkbox in Django?

开发者 https://www.devze.com 2022-12-13 17:33 出处:网络
How do I add different XHTML in a template for a checkbox field? I would like the following to work. I\'ve tried to use \"is_checkbox\" in the example below but it doesn\'t work because this isn\'t an

How do I add different XHTML in a template for a checkbox field? I would like the following to work. I've tried to use "is_checkbox" in the example below but it doesn't work because this isn't an admin form.

{% for field in payment_form %}
  <p class="{% if field.is_checkbox %}checkboxes {% endif %} ">
    {% if field.is_checkbox %}
      {{ field }开发者_如何转开发} {{ field.label_tag }}
    {% else %}
      {{ field.label_tag }} {{ field }}
    {% endif %}
    <span class="error">{{ field.errors }}</span>
  </p>
  {% endfor %} 


Well, if it is indeed a custom form and not an admin form, you can always override the form fields/widgets for such attrs. Like,

class MyCheckboxInput(forms.CheckboxInput):
    def is_checkbox(self):
        return True

And use MyCheckboxInput in form class, instead of forms.CheckboxInput. It will give you, the is_checkbox attr on template, and then your code will work.


I would do this using a TemplateTag, a custom filter to format a form. of form {{ form|beautify_form }} Since all the form to be displayed can be edited , this way you get away with all the formatting delegated to a separate function.

0

精彩评论

暂无评论...
验证码 换一张
取 消