diff options
author | MR Lemon <matt@matthewlemon> | 2020-05-02 16:43:18 +0100 |
---|---|---|
committer | MR Lemon <matt@matthewlemon> | 2020-05-02 16:43:18 +0100 |
commit | 73a7322d6591ca76806e6e6f0dc7a4b2b1683a0c (patch) | |
tree | 8d30fb0584969db4a5d9e178e82f340f6fa198ab /ctrack/organisations | |
parent | bf044aedde89574fffdc052f66b845b9bdb75b51 (diff) |
working with one crispy form
Diffstat (limited to 'ctrack/organisations')
-rw-r--r-- | ctrack/organisations/forms.py | 39 | ||||
-rw-r--r-- | ctrack/organisations/templates/organisations/org_create_formset.html | 6 | ||||
-rw-r--r-- | ctrack/organisations/views.py | 4 |
3 files changed, 19 insertions, 30 deletions
diff --git a/ctrack/organisations/forms.py b/ctrack/organisations/forms.py index ac96bcb..2fe5033 100644 --- a/ctrack/organisations/forms.py +++ b/ctrack/organisations/forms.py @@ -1,5 +1,5 @@ from crispy_forms.helper import FormHelper -from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit, Button, Field +from crispy_forms.layout import Layout, Fieldset, Field, ButtonHolder, Submit from django import forms from django.forms import inlineformset_factory from django.urls import reverse @@ -24,10 +24,6 @@ class OrganisationCreateForm(forms.ModelForm): "comments", "active" ), - ButtonHolder( - Submit("submit", "Submit", css_class="btn-primary"), - Button("cancel", "Cancel", onclick=f"location.href='{cancel_redirect}';", css_class="btn-danger") - ) ) class Meta: @@ -46,32 +42,27 @@ class OrganisationCreateForm(forms.ModelForm): class AddressCreateForm(forms.ModelForm): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - cancel_redirect = reverse("organisations:list") - self.helper = FormHelper(self) - self.helper.layout = Layout( - Fieldset( - "Add an address", - "type", - "line1", - "line2", - "line3", - "city", - "county", - "postcode", - "country", - "other_details" - ) - ) - class Meta: model = Address fields = ('type', 'line1', 'line2', 'line3', 'city', 'county', 'postcode', 'country', 'other_details') +# https://dev.to/zxenia/django-inline-formsets-with-class-based-views-and-crispy-forms-14o6 +# good advice on setting up the inlineformset - with crispy forms too AddressInlineFormSet = inlineformset_factory(Organisation, Address, fields=("type", "line1", "line2", "line3", "city", "county", "postcode", "country", "other_details"), form=AddressCreateForm) + + +class OrganisationInlineFormSetHelper(FormHelper): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.form_method = "post" + self.layout = Layout( + "line1", + "line2" + ) + self.render_required_fields = True + self.add_input(Submit("submit", "Save")) diff --git a/ctrack/organisations/templates/organisations/org_create_formset.html b/ctrack/organisations/templates/organisations/org_create_formset.html index 8b57bc9..393b94e 100644 --- a/ctrack/organisations/templates/organisations/org_create_formset.html +++ b/ctrack/organisations/templates/organisations/org_create_formset.html @@ -14,11 +14,7 @@ <form method="post" action=""> {% csrf_token %} {{ form }} - <table> - {{ addresses.management_form }} - {{ addresses }} - </table> - <input type="submit" value="SUBMIT"> + {% crispy addresses helper %} </form> </div> </div> diff --git a/ctrack/organisations/views.py b/ctrack/organisations/views.py index 427d385..925ac77 100644 --- a/ctrack/organisations/views.py +++ b/ctrack/organisations/views.py @@ -1,12 +1,13 @@ from typing import Any from typing import Dict +from crispy_forms.layout import Submit from django.contrib.auth.mixins import LoginRequiredMixin from django.db import transaction from django.urls import reverse_lazy from django.views.generic import DetailView, ListView, CreateView -from .forms import OrganisationCreateForm, AddressInlineFormSet +from .forms import OrganisationCreateForm, AddressInlineFormSet, OrganisationInlineFormSetHelper from .models import Organisation @@ -21,6 +22,7 @@ class OrganisationCreate(CreateView): context["addresses"] = AddressInlineFormSet(self.request.POST) else: context["addresses"] = AddressInlineFormSet() + context["helper"] = OrganisationInlineFormSetHelper() return context def form_valid(self, form): |