Fix UPS host form with custom form class for date handling

This commit is contained in:
xiaji
2026-04-30 14:46:57 +08:00
parent 7f0112d400
commit 749ab06ee7
3 changed files with 28 additions and 9 deletions

View File

@@ -3,6 +3,30 @@ from django.forms import inlineformset_factory
from .models import UPSHost, Battery 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 BatteryForm(forms.ModelForm):
class Meta: class Meta:
model = Battery model = Battery

View File

@@ -87,12 +87,7 @@
<label for="{{ form.last_maintenance_date.id_for_label }}" class="form-label font-weight-semibold"> <label for="{{ form.last_maintenance_date.id_for_label }}" class="form-label font-weight-semibold">
{{ form.last_maintenance_date.label }} {{ form.last_maintenance_date.label }}
</label> </label>
<input type="text" {{ form.last_maintenance_date }}
id="{{ form.last_maintenance_date.id_for_label }}"
name="{{ form.last_maintenance_date.name }}"
class="form-control datepicker"
{% if form.instance.last_maintenance_date %}value="{{ form.instance.last_maintenance_date|date:'Y-m-d' }}"{% endif %}
placeholder="选择日期">
{% if form.last_maintenance_date.errors %} {% if form.last_maintenance_date.errors %}
{% for error in form.last_maintenance_date.errors %} {% for error in form.last_maintenance_date.errors %}
<div class="invalid-feedback d-block">{{ error }}</div> <div class="invalid-feedback d-block">{{ error }}</div>

View File

@@ -3,7 +3,7 @@ from django.views.generic import ListView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.contrib import messages from django.contrib import messages
from .models import UPSHost, Battery, Contact, Supplier, MaintenanceRecord from .models import UPSHost, Battery, Contact, Supplier, MaintenanceRecord
from .forms import BatteryFormSet from .forms import BatteryFormSet, UPSHostForm
class DashboardView(ListView): class DashboardView(ListView):
@@ -99,14 +99,14 @@ class UPSHostListView(ListView):
class UPSHostCreateView(CreateView): class UPSHostCreateView(CreateView):
model = UPSHost model = UPSHost
template_name = 'ups_manager/ups_form.html' 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') success_url = reverse_lazy('ups_list')
class UPSHostUpdateView(UpdateView): class UPSHostUpdateView(UpdateView):
model = UPSHost model = UPSHost
template_name = 'ups_manager/ups_form.html' 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') success_url = reverse_lazy('ups_list')
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):