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

View File

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

View File

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