Created
July 27, 2014 09:18
-
-
Save nitely/39f49aab77a79026dcb7 to your computer and use it in GitHub Desktop.
Django template filter for checking if a field is a BooleanField (checkbox)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #-*- coding: utf-8 -*- | |
| from django.forms.widgets import CheckboxInput | |
| from .. import register | |
| @register.filter | |
| def is_checkbox(field): | |
| if isinstance(field.field.widget, CheckboxInput): | |
| return True | |
| else: | |
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #-*- coding: utf-8 -*- | |
| from django.test import TestCase | |
| class UtilsTemplateTagTests(TestCase): | |
| def test_is_checkbox(self): | |
| """ | |
| return True if the field is a checkbox | |
| """ | |
| class CheckBoxForm(forms.Form): | |
| checkbox = forms.BooleanField() | |
| nocheckbox = forms.CharField() | |
| out = Template( | |
| "{% load spirit_tags %}" | |
| "{% for field in form.visible_fields %}" | |
| "{{ field|is_checkbox }}," | |
| "{% endfor %}" | |
| ).render(Context({'form': CheckBoxForm(), })) | |
| self.assertEqual(out.strip(), "True,False,") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import .. registeris: