diff --git a/bds/templates/bds/forms/checkbox.html b/bds/templates/bds/forms/checkbox.html new file mode 100644 index 00000000..109ba75c --- /dev/null +++ b/bds/templates/bds/forms/checkbox.html @@ -0,0 +1,16 @@ +
+ {% if field.auto_id %} + + {% endif %} + {% for error in field.errors %} + {{ error }} + {% endfor %} + + {% if field.help_text %} +

+ {{ field.help_text|safe }} +

+ {% endif %} +
\ No newline at end of file diff --git a/bds/templates/bds/forms/field.html b/bds/templates/bds/forms/field.html new file mode 100644 index 00000000..132d6520 --- /dev/null +++ b/bds/templates/bds/forms/field.html @@ -0,0 +1,33 @@ +{% load bulma_utils %} + +
+ {% if field|is_checkbox %} + + {% include "bds/forms/checkbox.html" with field=field %} + + {% elif field|is_radio %} + + {% include "bds/forms/radio.html" with field=field %} + + {% elif field|is_input %} + + {% include "bds/forms/input.html" with field=field %} + + {% elif field|is_textarea %} + + {% include "bds/forms/textarea.html" with field=field %} + + {% elif field|is_select %} + + {% include "bds/forms/select.html" with field=field %} + + {% elif field|is_file %} + + {% include "bds/forms/file.html" with field=field %} + + {% else %} + + {% include "bds/forms/other.html" with field=field %} + + {% endif %} +
\ No newline at end of file diff --git a/bds/templates/bds/forms/file.html b/bds/templates/bds/forms/file.html new file mode 100644 index 00000000..e7390e04 --- /dev/null +++ b/bds/templates/bds/forms/file.html @@ -0,0 +1,31 @@ +{% load bulma_utils %} +{% load i18n %} + + + +
+ + + + {% for error in field.errors %} + {{ error }} + {% endfor %} + + {% if field.help_text %} +

+ {{ field.help_text|safe }} +

+ {% endif %} +
\ No newline at end of file diff --git a/bds/templates/bds/forms/form.html b/bds/templates/bds/forms/form.html new file mode 100644 index 00000000..fdc9dc00 --- /dev/null +++ b/bds/templates/bds/forms/form.html @@ -0,0 +1,22 @@ +{% if errors %} + {% if form.non_field_errors %} +
+
+ +
+
+ {% for non_field_error in form.non_field_errors %} + {{ non_field_error }} + {% endfor %} +
+
+ {% endif %} +{% endif %} + +{% for field in form.hidden_fields %} + {{ field }} +{% endfor %} + +{% for field in form.visible_fields %} + {% include 'bds/forms/field.html' %} +{% endfor %} \ No newline at end of file diff --git a/bds/templates/bds/forms/input.html b/bds/templates/bds/forms/input.html new file mode 100644 index 00000000..87afdeb8 --- /dev/null +++ b/bds/templates/bds/forms/input.html @@ -0,0 +1,19 @@ +{% load bulma_utils %} + + + +
+ {{ field|addclass:'input' }} + + {% for error in field.errors %} + {{ error }} + {% endfor %} + + {% if field.help_text %} +

+ {{ field.help_text|safe }} +

+ {% endif %} +
\ No newline at end of file diff --git a/bds/templates/bds/forms/other.html b/bds/templates/bds/forms/other.html new file mode 100644 index 00000000..4e1cdc8e --- /dev/null +++ b/bds/templates/bds/forms/other.html @@ -0,0 +1,19 @@ +{% if field.auto_id %} + +{% endif %} + +
+ {{ field }} + + {% for error in field.errors %} + {{ error }} + {% endfor %} + + {% if field.help_text %} +

+ {{ field.help_text|safe }} +

+ {% endif %} +
\ No newline at end of file diff --git a/bds/templates/bds/forms/radio.html b/bds/templates/bds/forms/radio.html new file mode 100644 index 00000000..d2e3b141 --- /dev/null +++ b/bds/templates/bds/forms/radio.html @@ -0,0 +1,24 @@ +{% if field.auto_id %} + +{% endif %} + +
+ {% for choice in field %} + + {% endfor %} + + {% for error in field.errors %} + {{ error }} + {% endfor %} + + {% if field.help_text %} +

+ {{ field.help_text|safe }} +

+ {% endif %} +
\ No newline at end of file diff --git a/bds/templates/bds/forms/select.html b/bds/templates/bds/forms/select.html new file mode 100644 index 00000000..2a064311 --- /dev/null +++ b/bds/templates/bds/forms/select.html @@ -0,0 +1,21 @@ +{% load bulma_utils %} + + + +
+ + {{ field }} + + + {% for error in field.errors %} + {{ error }} + {% endfor %} + + {% if field.help_text %} +

+ {{ field.help_text|safe }} +

+ {% endif %} +
\ No newline at end of file diff --git a/bds/templates/bds/forms/textarea.html b/bds/templates/bds/forms/textarea.html new file mode 100644 index 00000000..66e3a096 --- /dev/null +++ b/bds/templates/bds/forms/textarea.html @@ -0,0 +1,17 @@ + + +
+ {{ field|addclass:'textarea' }} + + {% for error in field.errors %} + {{ error }} + {% endfor %} + + {% if field.help_text %} +

+ {{ field.help_text|safe }} +

+ {% endif %} +
\ No newline at end of file diff --git a/bds/templatetags/bulma_utils.py b/bds/templatetags/bulma_utils.py new file mode 100644 index 00000000..b8e40bd9 --- /dev/null +++ b/bds/templatetags/bulma_utils.py @@ -0,0 +1,66 @@ +from django import forms, template + +register = template.Library() + + +@register.filter +def widget_type(field): + return field.field.widget + + +@register.filter +def is_select(field): + return isinstance(field.field.widget, forms.Select) + + +@register.filter +def is_multiple_select(field): + return isinstance(field.field.widget, forms.SelectMultiple) + + +@register.filter +def is_textarea(field): + return isinstance(field.field.widget, forms.Textarea) + + +@register.filter +def is_input(field): + return isinstance( + field.field.widget, + ( + forms.TextInput, + forms.NumberInput, + forms.EmailInput, + forms.PasswordInput, + forms.URLInput, + ), + ) + + +@register.filter +def is_checkbox(field): + return isinstance(field.field.widget, forms.CheckboxInput) + + +@register.filter +def is_multiple_checkbox(field): + return isinstance(field.field.widget, forms.CheckboxSelectMultiple) + + +@register.filter +def is_radio(field): + return isinstance(field.field.widget, forms.RadioSelect) + + +@register.filter +def is_file(field): + return isinstance(field.field.widget, forms.FileInput) + + +@register.filter +def addclass(field, css_class): + if len(field.errors) > 0: + css_class += " is-danger" + field_classes = field.field.widget.attrs.get("class", "") + field_classes += f" {css_class}" + return field.as_widget(attrs={"class": field_classes})