由于checkbox
,我正在寻找表行的值,但我并没有克服根据选择的复选框获取变量的问题。我必须添加-我是CBV的新手!
这是我的template.html:
<table class="table table-bordered table-striped table-condensed table_model" style="width: 160%;">
<thead>
<tr>
<th style="width: 10%;">Choice</th>
<th>Document title</th>
</tr>
</thead>
<tbody>
{% for document in query_document %}
<tr>
<td><input type="checkbox" name="DocumentChoice" value="{{ document.id }}"> </td>
<td>{{ document.title }}</td>
</tr>
{% endfor %}
</tbody>
</table>
在我的main.py文件中(类似于views.py文件)
class Document(View):
template_name = 'doc_form.html'
form_class = DocumentForm
success_url = 'doc_form.html'
def get(self, request):
form = self.form_class()
query_document = None
query_document_updated = None
if "SearchOMCL" in request.GET:
... some things ...
checkbox_id = request.POST.getlist('DocumentChoice')
print(checkbox_id)
context = {
'form': form,
'query_document' : query_document,
'query_document_updated' : query_document_updated
}
return render(request, self.template_name, context)
选中我的复选框后,我没有克服无法获得值列表的问题。我必须添加其他东西吗?
我这样修改了模板:
<form action="" method="get"> {%csrf_token%}
<td><input type="checkbox" name="DocumentChoice" value="{{ document.id }}"> </td>
<td>{{ document.title }}</td>
</tr>
{% endfor %}
<button class="btn btn-default" type="submit" name="SelectDocument">Submit</button>
</form>
我的main.py文件是这样的:
if "SelectDocument" in request.GET :
checkbox_id = request.GET['DocumentChoice']
print(checkbox_id)
我战胜了id
,但我认为这很丑对吗?