diff --git a/testapp/forms/person.py b/testapp/forms/person.py index d343f1e6..b00e7453 100644 --- a/testapp/forms/person.py +++ b/testapp/forms/person.py @@ -3,6 +3,8 @@ from django.core.exceptions import ValidationError from django.forms import fields, forms, models, widgets +from formset.renderers.bootstrap import FormRenderer as BootstrapFormRenderer +from formset.utils import FormMixin from formset.widgets import DateInput, Selectize, SelectizeMultiple, UploadedFileInput from testapp.models import PersonModel @@ -38,6 +40,18 @@ def clean(self): return cd +class PersonFormBootstrapRenderer(FormMixin, PersonForm): + """ + This form class shows how to use a custom renderer. The form then can be rendered using ``{{ form }}`` + """ + default_renderer = BootstrapFormRenderer( + field_css_classes='row mb-3', + label_css_classes='col-sm-3', + control_css_classes='col-sm-9', + ) + + + class ButtonActionsForm(forms.Form): """ This is a simple Django Form with just one input field. It is used to show how to use diff --git a/testapp/templates/bootstrap/person-form.html b/testapp/templates/bootstrap/person-form.html new file mode 100644 index 00000000..006ad50c --- /dev/null +++ b/testapp/templates/bootstrap/person-form.html @@ -0,0 +1,9 @@ +{% extends "bootstrap/base.html" %} +{% load render_form from formsetify %} + +{% block main-content %} + + {% render_form form "bootstrap" field_classes="row mb-3" label_classes="col-sm-3" control_classes="col-sm-9" %} + {% include "bootstrap/buttons.html" %} + +{% endblock %} \ No newline at end of file diff --git a/testapp/views.py b/testapp/views.py index 7f8b52ed..0e21e5fa 100644 --- a/testapp/views.py +++ b/testapp/views.py @@ -38,7 +38,8 @@ from testapp.forms.complete import CompleteForm from testapp.forms.contact import ( SimpleContactCollection, ContactCollection, ContactCollectionList, IntermediateContactCollectionList, - SortableContactCollection, SortableContactCollectionList) + SortableContactCollection, SortableContactCollectionList +) from testapp.forms.birthdate import BirthdateBoxForm, BirthdateCalendarForm, BirthdateInputForm, BirthdatePickerForm from testapp.forms.booking import BookingBoxForm, BookingCalendarForm, BookingPickerForm from testapp.forms.country import CountryForm @@ -49,7 +50,9 @@ from testapp.forms.moment import MomentBoxForm, MomentCalendarForm, MomentInputForm, MomentPickerForm from testapp.forms.moon import MoonForm, MoonCalendarRenderer from testapp.forms.opinion import OpinionForm -from testapp.forms.person import ButtonActionsForm, sample_person_data, ModelPersonForm +from testapp.forms.person import ( + ButtonActionsForm, sample_person_data, ModelPersonForm, PersonForm, PersonFormBootstrapRenderer, +) from testapp.forms.phone import PhoneForm from testapp.forms.poll import ModelPollForm, PollCollection from testapp.forms.profile import ProfileCollection @@ -173,7 +176,8 @@ def form_valid(self, form): def get_form_class(self): form_class = super().get_form_class() - assert not issubclass(form_class, FormMixin) + if issubclass(form_class, FormMixin): + return form_class attrs = self.get_css_classes() attrs.pop('button_css_classes', None) renderer_class = import_string(f'formset.renderers.{self.framework}.FormRenderer') @@ -612,6 +616,14 @@ class CompleteForm(FormMixin, forms.Form): form_class=ModelPersonForm, model=PersonModel, ), name='person'), + path('person-bootstrap-renderer', DemoFormView.as_view( + form_class=PersonFormBootstrapRenderer, + template_name='testapp/extended-form.html', + ), name='person-bootstrap-renderer'), + path('person-bootstrap-params', DemoFormView.as_view( + form_class=PersonForm, + template_name='bootstrap/person-form.html', + ), name='person-bootstrap-params'), path('poll', DemoModelFormView.as_view( form_class=ModelPollForm, model=PollModel,