【问题标题】:Django: Delete checked items using generic DeleteView?Django:使用通用 DeleteView 删除选中的项目?
【发布时间】:2013-03-08 11:57:56
【问题描述】:

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

如何在 Django 中使用通用视图 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 解决这个问题

【问题讨论】:

    标签: python django django-templates django-views django-generic-views


    【解决方案1】:

    DeleteView 是 SingleObjectMixin 的子类,所以它只能用于删除一个单一的对象。

    你应该:

    • 使用基于 ajax 的删除

    • 创建一个BundledDeleteListView,基于BaseListView,执行捆绑 删除。

    编码不错!

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2020-10-04
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多