修改通用list、form的bug

This commit is contained in:
王思川 2024-06-06 21:27:24 +08:00
parent 6d25fc9ac4
commit 2c0c433fbc
3 changed files with 34 additions and 34 deletions

View File

@ -20,7 +20,7 @@ def emp_list_view(request):
if name:
query_set = query_set.filter(name__icontains=name)
if department:
query_set = query_set.filter(department=request.GET.get('department', ''))
query_set = query_set.filter(primary_department=department)
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
@ -153,7 +153,7 @@ def emp_list_add(request):
def emp_list_modify(request):
if request.method == 'POST':
if 'id' in request.POST:
instance = EmployeeInformation.objects.get(id=request.POST['id'])
instance = EmployeeInformation.objects.get(employee_id=request.POST['id'])
form = EmployeeInformationForm(request.POST, instance=instance)
else:
form = EmployeeInformationForm(request.POST)
@ -164,7 +164,6 @@ def emp_list_modify(request):
else:
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
if 'id' in request.GET:
try:
@ -177,7 +176,6 @@ def emp_list_modify(request):
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html})
else:
return JsonResponse({"message": "无效的请求方法"}, status=405)

View File

@ -1,8 +1,8 @@
{% load tags %}
<form id="addEditForm" method="post">
{% csrf_token %}
{% if form.instance.id %}
<input type="hidden" name="id" value="{{ form.instance.id }}">
{% if form.instance.pk %}
<input type="hidden" name="id" value="{{ form.instance.pk }}">
{% endif %}
<div class="card-body">
<div class="row">
@ -11,25 +11,25 @@
{% if forloop.counter0|divisibleby:2 %}
<div class="mb-3">
<label class="form-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
{% if field|is_input_type:"text" %}
<input type="text" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value }}" placeholder="{{ field.label }}">
{% elif field|is_input_type:"number" %}
<input type="number" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value }}" placeholder="{{ field.label }}">
{% elif field|is_input_type:"select" %}
{% if field.field.widget.input_type == "text" %}
<input type="text" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "number" %}
<input type="number" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "select" %}
<select class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}">
{% for choice in field.field.choices %}
<option value="{{ choice.0 }}" {% if choice.0 == field.value %}selected{% endif %}>{{ choice.1 }}</option>
{% endfor %}
</select>
{% elif field|is_input_type:"textarea" %}
<textarea class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" rows="3">{{ field.value }}</textarea>
{% elif field|is_input_type:"date" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value }}">
{% elif field|is_input_type:"email" %}
<input type="email" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value }}" placeholder="{{ field.label }}">
{% elif field|is_input_type:"password" %}
<textarea class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" rows="3">{{ field.value|default_if_none:'' }}</textarea>
{% elif field.field.widget.input_type == "date" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d" }}">
{% elif field.field.widget.input_type == "email" %}
<input type="email" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "password" %}
<input type="password" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" placeholder="{{ field.label }}">
{% elif field|is_input_type:"checkbox" %}
{% elif field.field.widget.input_type == "checkbox" %}
<div class="form-check">
<input type="checkbox" class="form-check-input" id="{{ field.id_for_label }}" name="{{ field.name }}" {% if field.value %}checked{% endif %}>
<label class="form-check-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
@ -52,25 +52,25 @@
{% if not forloop.counter0|divisibleby:2 %}
<div class="mb-3">
<label class="form-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
{% if field|is_input_type:"text" %}
<input type="text" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value }}" placeholder="{{ field.label }}">
{% elif field|is_input_type:"number" %}
<input type="number" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value }}" placeholder="{{ field.label }}">
{% elif field|is_input_type:"select" %}
{% if field.field.widget.input_type == "text" %}
<input type="text" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "number" %}
<input type="number" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "select" %}
<select class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}">
{% for choice in field.field.choices %}
<option value="{{ choice.0 }}" {% if choice.0 == field.value %}selected{% endif %}>{{ choice.1 }}</option>
{% endfor %}
</select>
{% elif field|is_input_type:"textarea" %}
<textarea class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" rows="3">{{ field.value }}</textarea>
{% elif field|is_input_type:"date" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value }}">
{% elif field|is_input_type:"email" %}
<input type="email" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value }}" placeholder="{{ field.label }}">
{% elif field|is_input_type:"password" %}
<textarea class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" rows="3">{{ field.value|default_if_none:'' }}</textarea>
{% elif field.field.widget.input_type == "date" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d" }}">
{% elif field.field.widget.input_type == "email" %}
<input type="email" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "password" %}
<input type="password" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" placeholder="{{ field.label }}">
{% elif field|is_input_type:"checkbox" %}
{% elif field.field.widget.input_type == "checkbox" %}
<div class="form-check">
<input type="checkbox" class="form-check-input" id="{{ field.id_for_label }}" name="{{ field.name }}" {% if field.value %}checked{% endif %}>
<label class="form-check-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
@ -90,4 +90,8 @@
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
<button type="submit" class="btn btn-primary" id="submitForm">保存</button>
</div>
</form>

View File

@ -126,10 +126,6 @@
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
<button type="submit" class="btn btn-primary" id="submitForm">保存</button>
</div>
</div>
</div>
</div>
@ -185,6 +181,7 @@ $(document).ready(function(){
});
});
// 保存
function bindFormSubmit(){
$('#addEditForm').submit(function(e){
e.preventDefault();
@ -197,6 +194,7 @@ $(document).ready(function(){
$('#addEditModal').modal('hide');
showAlert('success', response.message);
// 你可以在这里刷新表格或更新页面内容
location.reload();
},
error: function(xhr){
// 处理表单错误并显示错误消息