From 749ab06ee7acfcd8365db934b64becb7b3f703e6 Mon Sep 17 00:00:00 2001 From: xiaji Date: Thu, 30 Apr 2026 14:46:57 +0800 Subject: [PATCH] Fix UPS host form with custom form class for date handling --- ups_management/ups_manager/forms.py | 24 +++++++++++++++++++ .../templates/ups_manager/ups_form.html | 7 +----- ups_management/ups_manager/views.py | 6 ++--- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/ups_management/ups_manager/forms.py b/ups_management/ups_manager/forms.py index 82a8076..78d60df 100644 --- a/ups_management/ups_manager/forms.py +++ b/ups_management/ups_manager/forms.py @@ -3,6 +3,30 @@ from django.forms import inlineformset_factory from .models import UPSHost, Battery +class UPSHostForm(forms.ModelForm): + class Meta: + model = UPSHost + fields = ['brand', 'model', 'ip_address', 'quantity', 'location', 'last_maintenance_date', 'contact'] + labels = { + 'brand': '品牌', + 'model': '型号', + 'ip_address': 'IP地址', + 'quantity': '数量', + 'location': '存放位置', + 'last_maintenance_date': '上次维保时间', + 'contact': '联系人', + } + widgets = { + 'brand': forms.TextInput(attrs={'class': 'form-control'}), + 'model': forms.TextInput(attrs={'class': 'form-control'}), + 'ip_address': forms.TextInput(attrs={'class': 'form-control'}), + 'quantity': forms.NumberInput(attrs={'class': 'form-control', 'min': '1'}), + 'location': forms.TextInput(attrs={'class': 'form-control'}), + 'last_maintenance_date': forms.DateInput(attrs={'class': 'form-control datepicker', 'type': 'text'}), + 'contact': forms.Select(attrs={'class': 'form-control'}), + } + + class BatteryForm(forms.ModelForm): class Meta: model = Battery diff --git a/ups_management/ups_manager/templates/ups_manager/ups_form.html b/ups_management/ups_manager/templates/ups_manager/ups_form.html index 94369e0..15f0650 100644 --- a/ups_management/ups_manager/templates/ups_manager/ups_form.html +++ b/ups_management/ups_manager/templates/ups_manager/ups_form.html @@ -87,12 +87,7 @@ - + {{ form.last_maintenance_date }} {% if form.last_maintenance_date.errors %} {% for error in form.last_maintenance_date.errors %}
{{ error }}
diff --git a/ups_management/ups_manager/views.py b/ups_management/ups_manager/views.py index 968e126..a40cff0 100644 --- a/ups_management/ups_manager/views.py +++ b/ups_management/ups_manager/views.py @@ -3,7 +3,7 @@ from django.views.generic import ListView, CreateView, UpdateView, DeleteView from django.urls import reverse_lazy from django.contrib import messages from .models import UPSHost, Battery, Contact, Supplier, MaintenanceRecord -from .forms import BatteryFormSet +from .forms import BatteryFormSet, UPSHostForm class DashboardView(ListView): @@ -99,14 +99,14 @@ class UPSHostListView(ListView): class UPSHostCreateView(CreateView): model = UPSHost template_name = 'ups_manager/ups_form.html' - fields = ['brand', 'model', 'ip_address', 'quantity', 'location', 'last_maintenance_date', 'contact'] + form_class = UPSHostForm success_url = reverse_lazy('ups_list') class UPSHostUpdateView(UpdateView): model = UPSHost template_name = 'ups_manager/ups_form.html' - fields = ['brand', 'model', 'ip_address', 'quantity', 'location', 'last_maintenance_date', 'contact'] + form_class = UPSHostForm success_url = reverse_lazy('ups_list') def get_context_data(self, **kwargs):