Update raid response formset with custom template, based on Crispy's default one, for better styling.

This commit is contained in:
Casper V. Kristensen 2019-10-27 16:52:08 +01:00
parent 80d109efc0
commit 74d3b3d3e9
Signed by: caspervk
GPG key ID: 289CA03790535054
2 changed files with 77 additions and 3 deletions

View file

@ -103,16 +103,16 @@ RaidResponseFormSet = inlineformset_factory(
Raid, # parent model Raid, # parent model
RaidResponse, RaidResponse,
fields=["character", "role", "status", "note", "attendance"], fields=["character", "role", "status", "note", "attendance"],
can_delete=False,
extra=1 extra=1
) )
class RaidResponseFormSetHelper(FormHelper): class RaidResponseFormSetHelper(FormHelper):
template = "bootstrap4/table_inline_formset.html" template = "raids/raid_response_table_inline_formset.html"
def __init__(self, form=None): def __init__(self, form=None):
super().__init__(form) super().__init__(form)
self.form_class = "responses_form"
self.layout = Layout( self.layout = Layout(
Field("character", css_class="character-select"), Field("character", css_class="character-select"),
Field("role", css_class="role-select"), Field("role", css_class="role-select"),
@ -121,4 +121,3 @@ class RaidResponseFormSetHelper(FormHelper):
Field("attendance", css_class="attendance-input"), Field("attendance", css_class="attendance-input"),
) )
self.form_tag = False self.form_tag = False

View file

@ -0,0 +1,75 @@
{% comment %}
Based on Crispy's default bootstrap4/table_inline_formset.html
https://github.com/django-crispy-forms/django-crispy-forms/blob/master/crispy_forms/templates/bootstrap4/table_inline_formset.html
{% endcomment %}
{% load crispy_forms_tags %}
{% load crispy_forms_utils %}
{% load crispy_forms_field %}
{% specialspaceless %}
{% if formset_tag %}
<form {{ flat_attrs|safe }} method="{{ form_method }}" {% if formset.is_multipart %} enctype="multipart/form-data"{% endif %}>
{% endif %}
{% if formset_method|lower == 'post' and not disable_csrf %}
{% csrf_token %}
{% endif %}
<div>
{{ formset.management_form|crispy }}
</div>
<table{% if form_id %} id="{{ form_id }}_table"{% endif%} class="table table-sm">
<thead>
{% if formset.readonly and not formset.queryset.exists %}
{% else %}
<tr>
{% for field in formset.forms.0 %}
{% if field.label and not field.is_hidden %}
<th for="{{ field.auto_id }}" class="col-form-label {% if field.field.required %}requiredField{% endif %}">
{{ field.label|safe }}{% if field.field.required and not field|is_checkbox %}<span class="asteriskField">*</span>{% endif %}
</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
</thead>
<tbody>
<tr class="d-none empty-form">
{% for field in formset.empty_form %}
{% include 'bootstrap4/field.html' with tag="td" form_show_labels=False %}
{% endfor %}
</tr>
{% regroup formset by status.value as status_forms_list %}
{% for status, status_forms in status_forms_list %}
{% regroup status_forms by role.value as role_forms_list %}
{% for role, role_forms in role_forms_list %}
{% for form in role_forms %}
{% if form_show_errors and not form.is_extra %}
{% include "bootstrap4/errors.html" %}
{% endif %}
<tr>
{% for field in form %}
{% include 'bootstrap4/field.html' with tag="td" form_show_labels=False %}
{% endfor %}
</tr>
{% endfor %}
{% if not forloop.last %}
<tr><td class="p-2"></td></tr>
{% endif %}
{% endfor %}
{% if not forloop.last %}
<tr><td class="pt-3"></td></tr>
<tr class="border-bottom"></tr>
<tr><td class="pb-3"></td></tr>
{% endif %}
{% endfor %}
</tbody>
</table>
{% include "bootstrap4/inputs.html" %}
{% if formset_tag %}</form>{% endif %}
{% endspecialspaceless %}