我最近在学习 Django。我用 SQLite 数据库创建了一个模型,并用模型创建了视图。所以我需要编辑页面中模型中的所有对象。像这样的东西:
更新按钮是否只是所有对象的一个按钮或每个对象的一个按钮都没关系。
这是html页面(edit_info.html):
<table class="table">
<thead class="thead-primary">
<tr>
<th class="">نام</th>
<th class="">نام خانوادگی</th>
<th class="cell100 column2">جنسیت</th>
<th class="cell100 column3">کد ملی</th>
<th class="cell100 column4">شماره پرسنلی</th>
<th class="cell100 column5">تلفن</th>
<th class="cell100 column6">آدرس</th>
<th class="cell100 column7">وضعیت تاهل</th>
<th class="cell100 column8">سن</th>
<th class="cell100 column9">حقوق دریافتی</th>
<th class="cell100 column10">ویرایش</th>
</tr>
</thead>
<tbody>
{% for employee in employees %}
<tr class="row100 body">
<form method="POST" class="post-form" action="/update/{{employee.personnel_code}}">
{% csrf_token %}
<th class="cell100 column0"><input type="text" name="first_name" id="id_first_name" required
maxlength="100" value="{{ employee.first_name }}" /></th>
<th class="cell100 column1"><input type="text" name="last_name" id="id_last_name" required
maxlength="100" value="{{ employee.last_name }}" /></th>
<th class="cell100 column2">
<input {% if employee.gender %} checked {% endif %} type="checkbox" name="gender" id="id_gender" />
</th>
<th class="cell100 column3"><input type="number" name="eid" id="id_eid" required maxlength="20"
value="{{ employee.eid }}" /></th>
<th class="cell100 column4"><input type="text" name="personnel_code" id="id_personnel_code" required
maxlength="6" value="{{ employee.personnel_code }}" /></th>
<th class="cell100 column5"><input type="text" name="phone_number" id="id_phone_number" required
maxlength="15" value="{{ employee.phone_number }}" /></th>
<th class="cell100 column6"><input type="text" name="address" id="id_address" required maxlength="1000"
value="{{ employee.address }}" /></th>
<th class="cell100 column7">
<input {% if employee.is_married %} checked {% endif %} type="checkbox" name="is_married"
id="id_is_married" />
</th>
<th class="cell100 column8"><input type="number" name="age" id="id_age" required maxlength="3"
value="{{ employee.age }}" /></th>
<th class="cell100 column9"><input type="number" name="salary" id="id_salary" required maxlength="10"
value="{{ employee.salary }}" /></th>
<th class="cell100 column10"><button type="submit" class="btn btn-success">
بروزرسانی
</button></th>
</form>
</tr>
{% endfor %}
</tbody>
</table>
这是更新方法和此编辑页面的视图:
def edit_info(request):
employees = Employee.objects.all()
return render(request, "edit_info.html", {'employees': employees})
def update(request, id):
employee = Employee.objects.get(personnel_code=id)
form = EmployeeForm(request.POST, instance=employee)
if form.is_valid():
form.save()
return redirect("/main")
print(form.errors.as_text)
employees = Employee.objects.all()
return render(request, 'edit_info.html', {'employees': employees})
最后是网址:
from django.contrib import admin
from django.urls import path
from employee import views
urlpatterns = [
path('', views.login),
path('admin/', admin.site.urls),
path('main', views.main),
path('home', views.home),
path('edit_info', views.edit_info),
path('update/<int:id>', views.update),
]
使用当前代码没有任何反应。请帮忙!