gpt4 book ai didi

python - Django:使用通用DeleteView删除选中的项目?

转载 作者:太空宇宙 更新时间:2023-11-03 19:03:18 25 4
gpt4 key购买 nike

我有一个表格,其中列出了我的所有项目,分为两列(删除、URL 名称)。在删除列中,每个项目都有一个复选框,还有一个用于选择所有项目的复选框。

如何在 Django 中使用通用 View DeleteView 来实现它?可能吗?

我的例子:

models.py

class UrlStatus_Proxy(models.Model):

urls = models.URLField(u'Site URL', max_length=100, null=True, unique=True)
status_url = models.CharField(u'Site', max_length=20, choices=STATUS_URL)

def __unicode__(self):
return self.urls

views.py 类 ProxyUrlListView(ListView):

    model = UrlStatus_Proxy

def get_context_data(self, **kwargs):
context = super(ProxyUrlListView,
self).get_context_data(**kwargs)

context['allow_urls'] = UrlStatus_Proxy.objects.filter(status_url__startswith='allow')
context['block_urls'] = UrlStatus_Proxy.objects.filter(status_url__startswith='block')
return context

urls.py

url(r'^url_status/(?P<pk>\d+)/delete/?$',ProxyUrlDeleteView.as_view(),name='delete_proxy'),
url(r'url_status/$', ProxyUrlListView.as_view(template_name='example.html'))

此时我已经为 block_urls 对象编写了模板,因为这足以解决问题。

example.html

<form action="" method="post" enctype="multipart/form-data"> {% csrf_token %} {{ form.errors }}
{% if block_urls %}
<li>
{% csrf_token %}
<div class="hastable">
<table>
<thead>
<tr>
<th>{% trans 'Site Url' %}</th>
<th><label>{% trans 'Delete' %}</label><input type="checkbox" value="check_one" onclick="check(this)" class="submit"/></th>
</tr>
</thead>
<tbody>
{% for block in block_urls %}
<tr>
<td style="text-align: center">{{ block.urls }}</td>
<td style="text-align: center"><input type="checkbox" value="{{ block.id }}" name="list" class="checkbox"/></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div style="text-align: right">
<input type="submit" id="delete_selected_block" name="delete_selected_block " value="Delet selected" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">

</div>
</li>
{% endif %}
</form>

如何使用上面在urls.py中显示的DeleteView解决此问题

最佳答案

DeleteView是SingleObjectMixin的子类,因此只能用于删除单个对象。

你应该:

  • 使用基于ajax的删除

  • 基于BaseListView创建BundledDeleteListView,执行捆绑操作删除。

良好的编码!

关于python - Django:使用通用DeleteView删除选中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com