{% load crispy_forms_tags %}

{{ errors }}
{% if errors and not fields %}
  <p>{% for field in hidden_fields %}{{ field }}{% endfor %}</p>
{% endif %}
{% for field, errors in fields %}
  {% ifchanged field.field.widget.attrs.category %}
    {% if not forloop.first %}
    <hr>
    {% endif %}
    {% if field.field.widget.attrs.category %}
        <h3 class='col-md-12'>{{field.field.widget.attrs.category}}</h3>
    {% endif %}
  {% endifchanged %}
    <div{% with classes=field.css_classes %}{% if classes %} class="{{ classes }}"{% endif %}{% endwith %}>
      {{ field|as_crispy_field }}
      {% if forloop.last %}
        {% for field in hidden_fields %}{{ field }}{% endfor %}
      {% endif %}
      </div>
{% endfor %}
{% if not fields and not errors %}
  {% for field in hidden_fields %}{{ field }}{% endfor %}
{% endif %}
<button class="btn btn-primary mb-5" id="form_submit_{{form.instance.id}}" type="submit" hx-post="{% url 'django_form_generator:form_detail' form.instance.id %}">{{form.instance.submit_text}}</button> 
