Python 使用基于函数的视图获取pk_id
我在获取模板中的Python 使用基于函数的视图获取pk_id,python,django,python-3.x,postgresql,django-2.1,Python,Django,Python 3.x,Postgresql,Django 2.1,我在获取模板中的pk时遇到问题。当我选择一条记录时,它总是返回最后一个pkID。顺便说一句,我使用的是functional base view。以下是my collection.html: <form method="POST" action="{% url 'single_collection' %}"> {% csrf_token %} <table class="table" id="dataTables-example"> <thead> <
pk
时遇到问题。当我选择一条记录时,它总是返回最后一个pk
ID。顺便说一句,我使用的是functional base view。以下是my collection.html:
<form method="POST" action="{% url 'single_collection' %}">
{% csrf_token %}
<table class="table" id="dataTables-example">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Status</th>
<th>Download</th>
</tr>
</thead>
<tbody>
{% for collectionlist in collection %}
<tr>
<td>{{ collectionlist.id }}</td>
<td>{{ collectionlist.sqa_name }}</td>
<td>{{ collectionlist.status }}</td>
<td class="center"><center><button type="button" class="btn btn-link" data-toggle="modal" data-target="#myModaldl{{ collectionlist.id }}" ><span class="glyphicon glyphicon-download-alt"></span></button></center></td>
</tr>
<div class="modal fade collectClass" id="myModaldl{{ collectionlist.id }}" role="dialog" tabindex="-1">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Single Collect</h3>
</div>
<div class="modal-body form-horizontal">
<div class="form-group">
<label for="inputSQAID" class="col-sm-3 control-label">SQA Name</label>
<div class="col-sm-8">
<input type="hidden" name="pk_id" id="pk_id" value="{{ collectionlist.id }}">
<input type="text" class="form-control" name="singlecollect" value="{{ collectionlist.sqa_name }}" id="inputSQAID">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success" name="single_dl">Download</button>
</div>
</div>
</div>
</div>
{% endfor %}
</tbody>
</table>
</form>
在my views.py中,我只想首先使用模式打印我在表中选择的项/记录的
pk
ID。但是,它总是获取数据库中的最后一条记录 这是因为您有一个
标记,其中包含所有数据收集
行。您应该为每一张表格提供单独的表格,即:
{% for collectionlist in collection %}
<form method="POST" action="{% url 'single_collection' %}">
{% csrf_token %}
...
</form>
{% endfor %}
{%用于集合%中的collectionlist}
{%csrf_令牌%}
...
{%endfor%}
在表单中,您有多个同名输入。不能那样做。FormData键需要是唯一的,因为您多次使用相同的名称,最后一个键的值将丢弃其他键。
{% for collectionlist in collection %}
<form method="POST" action="{% url 'single_collection' %}">
{% csrf_token %}
...
</form>
{% endfor %}