From 5ae60be3b4a830b52333c5d715cbde57a1168428 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 15 Mar 2022 21:23:54 -0400 Subject: [PATCH] [rentals] Use widget_tweaks to apply CSS classes to form fields ... instead of subclassing ModelForm to apply a class to all fields --- rentals/forms.py | 9 +-------- rentals/templates/rentals/lockers.dj.html | 4 +++- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/rentals/forms.py b/rentals/forms.py index 0b15179..911fb06 100644 --- a/rentals/forms.py +++ b/rentals/forms.py @@ -3,14 +3,7 @@ from django import forms from rentals.models import LockerInfo -class BoostrapModelForm(forms.ModelForm): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - for field in self.fields.values(): - field.widget.attrs["class"] = "form-control" - - -class LockerInfoForm(BoostrapModelForm): +class LockerInfoForm(forms.ModelForm): class Meta: model = LockerInfo fields = [ diff --git a/rentals/templates/rentals/lockers.dj.html b/rentals/templates/rentals/lockers.dj.html index 4625504..152a9cb 100644 --- a/rentals/templates/rentals/lockers.dj.html +++ b/rentals/templates/rentals/lockers.dj.html @@ -1,5 +1,7 @@ {% extends "base.dj.html" %} +{% load widget_tweaks %} + {% block title %}Lockers Index{% endblock %} {% block admin_link %} {% url 'admin:app_list' 'rentals' %} @@ -81,7 +83,7 @@
{% for field in locker_form.visible_fields %}
- {{ field }} + {% render_field field class+="form-control" %} {{ field.label_tag }} {{ field.errors }} {% if field.help_text %}