我想发生的是,当我单击更新按钮时,我也可以在数据库中编辑模式的内容。现在,我无法做到这一点。我的模态在我的index.html中这是我的........
模态和index.html中的代码:
<!-- Modal -->
<div class="modal fade" id="employee.employee_id_{{ employee.employee_id }}" tabindex="-1" role="dialog" style="display: none; overflow: auto;" aria-hidden="true" data-backdrop="static">
<div class="modal-dialog" role="document">
<div class="modal-content">
<h5 class="modal-title" id="employee.employee_id_{{ employee.employee_id }}" </h5>
</div>
<div class="modal-body" style="overflow: auto">
<div class="container-fluid">
<div class="row">
<div class="col-xl-12">
<div class="card">
<div class="card-header-success" >
<h4 align="center" class="card-title">EMPLOYEE PROFILE</h4>
</div>
<form class="form" id="edit_employee_form" name="edit_employee_form" method="post" action="/edit_employee_form">
<div class="card">
<div class="card-body">
<div class="text-center">
<img src="{% static 'img/faces/marc.jpg' %}" class="rounded" height="150" width="150">
</div>
<br>
<div class="row">
<div class="col-md-6">
<input type="text" class="form-control" name="emp_id" value="{{employee.employee_id}}" hidden>
</div>
</div>
<div class="form-row">
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="first_name">First Name</label>
<input type="text" class="form-control" id="first_name" name="val_first_name" value=" {{ employee.first_name }}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="first_name">Middle Name</label>
<input type="text" class="form-control" id="middle_name" name="val_middle_name" value=" {{ employee.middle_name }}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="last_name">Last Name</label>
<input type="text" class="form-control" id="last_name" name="val_first_name" value=" {{ employee.last_name }}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="present_address">Present Address</label>
<input type="text" class="form-control" id="present_address" name="val_present_address" value=" {{ employee.present_address }}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="permanent_address">Permanent Address</label>
<input type="text" class="form-control" id="permanent_address" name="val_permanent_address" value=" {{ employee.permanent_address }}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="zipcode">Zipcode</label>
<input type="text" class="form-control" id="zipcode" name="val_zipcode" value=" {{ employee.zipcode }}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="birthday">Birthday</label>
<input type="text" class="form-control" id="birthday" name="val_birthday" value=" {{ employee.birthday }}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="email_address">Email</label>
<input type="text" class="form-control" id="email_address" name="val_email_address" value=" {{ employee.email_address }}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="user_type">User Type</label>
<input type="text" class="form-control" id="user_type" name="val_user_type" value=" {{ employee.user_type }}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="pagibig_id">Pagibig ID</label>
<input type="text" class="form-control" id="pagibig_id" name="val_pagibig_id" value=" {{ employee.pagibig_id}}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="sss_id">SSS ID</label>
<input type="text" class="form-control" id="sss_id" name="val_sss_id" value=" {{ employee.sss_id}}" >
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-group bmd-form-group is-focused">
<label class="bmd-label-floating" for="sss_id">TIN ID</label>
<input type="text" class="form-control" id="tin_id" name="val_tin_id" value=" {{ employee.tin_id}}" >
</div>
</div>
</div>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<a href="/edit_employee_form" type="submit" id="btm_update_emp" class="btn btn-success btn-round ">Update</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
此处是来自form.py和views.py表格.py
class EditEmployeeForm(ModelForm):
class Meta:
model = Employee
fields = ['employee', 'first_name', 'middle_name', 'last_name', 'present_address', 'permanent_address', 'zipcode', 'birthday', 'email_address', 'pagibig_id', 'sss_id', 'tin_id']
views.py(不确定其是否正确)
def edit_employee_form(request):
request.POST._mutable = True
form1 = EditEmployeeForm(request.POST)
first_name_val = request.POST['val_first_name']
middle_name_val = request.POST['val_middle_name']
last_name_val = request.POST['val_last_name']
primary_address_val = request.POST['val_primary_address']
permanent_address_val = request.POST['val_permanent_address']
zipcode_val = request.POST['val_zipcode']
email_address_val = request.POST['val_email_address']
pagibig_id_val = request.POST['val_pagibig_id']
tin_id_val = request.POST['val_tin_id']
sss_id_val = request.POST['val_sss_id']
form1.data['first_name'] = first_name_val
form1.data['middle_name'] = middle_name_val
form1.data['last_name'] = last_name_val
form1.data['primary_address'] = primary_address_val
form1.data['permanent_address'] = permanent_address_val
form1.data['zipcode'] = zipcode_val
form1.data['email_address'] = email_address_val
form1.data['pagibig_id'] = pagibig_id_val
form1.data['tin_id'] = tin_id_val
form1.data['sss_id'] = sss_id_val
if form1.is_valid():
form1.save()
sd = "Saved!"
else:
err = form1.errors
sd = "Error!"
context = {'form': form1,
'success_emp_edit': sd
}
return render(request, 'index.html', context)
和urls.py
path('edit_employee_form/', views.edit_employee_form)
使用通用视图轻松完成
views.py
from django.views.generic.edit import UpdateView
from django.urls import reverse_lazy
class EmployeeUpdate(UpdateView):
model = Employee
form_class = EditEmployeeForm
template_name = 'employee_form.html'
success_url = reverse_lazy('dashboard')
urls.py
path('employee/<int:employee_id>/', views.EmployeeUpdate.as_view(), name="employee-update")
和index.html中的更改
<a href="{% url 'employee-update' employee.id %}" id="btm_update_emp" class="btn btn-success btn-round ">Update</a>