我有一个模型实例表,如下所示:
_____________________
|1| | | |<a/>|
_____________________
|2| | | |<a/>|
_____________________
|3| | | |<a/>|
.......................
_____________________
Django使用for
模板循环生成它,第一列是模型实例的主键。最后一列是页面fith表单的链接,用于编辑此模型实例。我在views.py中有2个函数:呈现此页面(edit_employee()
)并提交更改(edit_employee_action()
)。在上一个中,我需要处理此主键。
用户从此处选择实例
<table>
<tr>
<th class="table-title" id="number">
№
</th>
<th class="table-title" id="second-name">
Фамилия
</th>
<th class="table-title" id="first-name">
Имя
</th>
<th class="table-title" id="patronymic">
Отчество
</th>
<th class="table-title" id="birth-date">
Дата рождения
</th>
</tr>
{% for employee in table %}
<tr>
{% for field in employee %}
<th>{{ field }}</th>
{% endfor %}
<th class="button-cell" id="edit"><a employee_id="{{ employee.0 }}" href="{% url 'edit-employee' %}">!</a></th>
<th class="button-cell negative" id="delete"><a href="{% url 'delete-employee' %}">-</a></th>
</tr>
{% endfor %}
</table>
表格
<form id="add-employee-form" action="edit/<int:id>/" method="POST">
<table>
<tr>
<th class="table-title" id="second-name">
Фамилия
</th>
<th class="table-title" id="first-name">
Имя
</th>
<th class="table-title" id="patronymic">
Отчество
</th>
<th class="table-title" id="birth-date">
Дата рождения
</th>
</tr>
<tr>
<th id="second-name">
<div class="field-wrapper">
{{ form.second_name.errors }}
{{ form.second_name }}
</div>
</th>
<th id="first-name">
<div class="field-wrapper">
{{ form.first_name.errors }}
{{ form.first_name }}
</div>
</th>
<th id="patronymic">
<div class="field-wrapper">
{{ form.patronymic.errors }}
{{ form.patronymic }}
</div>
</th>
<th id="birth-date">
<div class="field-wrapper" id="birth-date-wrapper">
{{ form.birth_date.errors }}
{{ form.birth_date }}
</div>
</th>
</tr>
</table>
{% csrf_token %}
urls.py
path('employee/edit_employee/', views.edit_employee, name='edit-employee'),
path('employee/edit_employee/edit/', views.edit_employee_action, name='edit-employee-action'),
views.py
def edit_employee(request):
form = AddEmployeeForm()
return render(
request,
'edit_employee.html',
context={'form': form}
)
def edit_employee_action(request, id):
if request.method == "POST":
form = AddEmployeeForm(request.POST)
if form.is_valid():
edited_employee = .............
edited_employee.update(
first_name = request.POST.first_name,
second_name = request.POST.second_name,
patronymic = request.POST.patronymic,
birth_date = request.POST.birth_date
)
else:
form = AddEmployeeForm()
form = AddEmployeeForm()
return render(
request,
'edit_employee.html',
context={'form': form}
)
views.py
?edit_employee()
传递到edit_employee_action()
?我可以创建全局变量,但这不是我想的最好方法。答案 0 :(得分:1)
在id
文件的视图路径中指定urls.py
参数:
path('edit_employee_action/<int:id>/', views.edit_employee_action, name='edit_employee_action'),
然后将参数添加到您的edit_employee_action
视图中:
from django.shortcuts import get_object_or_404
# your other imports...
def edit_employee_action(request, id):
employee = get_object_or_404(YourModel, id=id) # show a 404 error if the record doesn't exist.
# the rest of your code
最后将id
添加到表中的<a>
标记中:
...
<th class="button-cell" id="edit"><a method="post" href="{% url 'edit-employee' employee.id %}">!</a></th>
...