我想知道如何通过一次点击从输入框中编辑一次更新数据的方法。我发现这篇关于如何使用复选框的帖子: Updating several records at once using Django 但是我不确定如何为输入框做这件事。
这是我的代码:
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Local</th>
<th>line</th>
<th>rack</th>
</tr>
</thead>
<tbody>
{% for linea in lineas_de_reporte %}
<tr>
<td>{{ linea.local }}</td>
<td><input type="text" name="case-opened" value="{{ linea.rack }}" id="caseOpened"/></td>
<td><input type="text" name="case-opened" value="{{ linea.line }}" id="caseOpened"/></td>
</tr>
{% endfor %}
</tbody>
有没有办法只使用Django将所有值(按id)发送到列表中的函数?
编辑:
forms.py
class TVSForm(forms.Form):
rack = forms.CharField()
line = forms.CharField()
views.py
def search_folio(request):
if request.method == 'POST':
form = FolioSearchForm(request.POST)
if form.is_valid():
folio_number = request.POST.get('folio_number', '')
folio = 'FOLIO' + str(folio_number)
folio_query = InventoryFolioManagement.objects.filter(folio=folio)
context = {
'lineas_de_reporte': folio_query,
'folio': ' | ' + folio
}
return render(request, 'inv-search-folio.html', context)
else:
form = FolioSearchForm()
if request.GET.get('rack'):
# ... this means that user clicked on the submit button
# get the id of linea
linea_id = request.GET.get('rack-id')
new_case = request.GET.get('rack')
# fetch object from db, based on that id (aka pk)
linea = TechnicalValidationSystem.objects.get(id=linea_id)
# change its attribute to True
linea.case_opened = new_case
# update db with the new value
linea.save(update_fields=['rack'])
return render(request, 'inv-search-folio.html')