django - 删除django中的多行
问题描述
我试图在 django 中同时删除几行。
如何通过单击复选框来简单地更改布尔字段“to_delete”的状态?
我正在使用数据表。The way how I am doing it is to create a boolean to_delete in my model, when the checkbox is selected, I am calling the function delete_multiple_company in my view. 但是,这不起作用。任何想法,请我做错了什么。非常感谢,
我已经创建了我的视图:
视图.py
def delete_multiple_company(request, company_id):
company = get_object_or_404(Company, pk=company_id)
company = Company.objects.get(pk=company_id)
company.objects.filter(to_delete=True).delete()
return HttpResponseRedirect(reverse("company:index_company"))
网址.py
url(r'^(?P<company_id>[0-9]+)/delete_multiple_company/$', views.delete_multiple_company, name='delete_multiple_company'),
模型.py
class Company(models.Model):
to_delete = models.BooleanField(default=False)
索引.html
<a href="{% url 'company:delete_multiple_company' company.id %}" id="table" class="btn btn-default btn-sm active float: right" style="float: right;"><span class="fa fa-plus"></span>Delete Companies</a>
<table id="dtBasicExample" class="table table-striped table-hover">
<thead>
<tr>
<th>Select</th>
<th>#</th>
<th>Checked ?</th>
</tr>
</thead>
<tbody>
{% for company in companys.all %}
<tr>
<td id="{{ company.id }}"><input type="checkbox" class="companyCheckbox" name="checkedbox" id="{{ company.id }}" value="{{ company.id }}"></td>
<td>{{ company.id }}</td>
<td>{{ company.to_delete }}</td>
</tr>
{% endfor %}
</tbody>
</table>
解决方案
我遇到了和你类似的问题,我所做的就是放在一个表格中。
索引.py
<form method="POST" class="post-form">{% csrf_token %}
<button type="submit" class="save btn btn-default">Delete</button>
<span class="fa fa-plus"></span>Delete Companies</a>
<table id="dtBasicExample" class="table table-striped table-hover">
<thead>
<tr>
<th>Select</th>
<th>#</th>
<th>Checked ?</th>
</tr>
</thead>
<tbody>
{% for company in companys.all %}
<tr>
<td id="{{ company.id }}"><input type="checkbox" class="companyCheckbox" name="checkedbox" id="{{ company.id }}" value="{{ company.id }}"></td>
<td>{{ company.id }}</td>
<td>{{ company.to_delete }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" class="save btn btn-default">Delete</button>
</form>
在我看来,单击按钮然后触发 POST 方法。
视图.py
def index(request):
if request.method == 'POST':
print('I made it here')
# Put your code here, note you will return a dict, so some trial and error should be expected
推荐阅读
- ios - iOS 11 Single App Mode, Disable lock screen
- apache-spark - 部分更新Dataframe中记录的有效方法
- c# - c#通过内存地址调用方法
- javascript - Mask unmask in textfield
- events - Microsoft Graph:获取事件中的日历信息
- r - 如何将 bquote 与 ggplot2 geom_label 结合使用?
- javascript - Bootstrap Validator - RegExp weird behavior
- java - What is the best practise in spring rest for coding Id based and name based API
- c++ - How to check whether rectangle is golden rectangle using Fibonacci and recursion function?
- java - Android libgdx: NullPointerException on SQLiteOpenHelper database