How do I add different content for a checkbox in Django?
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.
精彩评论