如何在Django中删除多个删除

时间:2018-06-21 09:01:50

标签: javascript django django-forms django-templates django-views

怀疑:

在这里我提到了HTML和输出图像。

如果我单击全选选项(复选框),它将选择所有图像,但是我怀疑是一旦单击删除按钮,如何删除所有图像。

HTML

{% for j in img %}
    <h4>
        Select All
        <input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" />
    </h4>
    <button class="btn btn-danger" action=" ">Delete</button>
    <div class="col">
        {% if i.placename == j.gallery_place %}
            <div class="show-image">
                <img src="{{j.gallery_image.url}}" style="height:130px; width:130px;" />

                <tag class="one" style="margin:8%">
                    <input type="checkbox" name="selected[]" value="{{j.id}}" />
                </tag>

            </div>
        {% endif %}
    </div>
{% endfor %}

图片

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以在Django中使用FormView并按以下方式处理它们。您需要先创建带有复选框的表单:

# forms.py
class CheckboxesForm(forms.Form):
    checkboxes = forms.ModelMultipleChoiceField(
        MyModel.objects.all(),
        widget=forms.CheckboxSelectMultiple)

然后,您需要编写自己的FormView并重写form_valid()方法以执行所选对象的删除。如果要定义具有复选框的对象,则可以重写get_form()方法。

# views.py
class MyListView(ListView):
    """
    View displaying a list of objects, you will redirect here after successfully deleting the objects you want
    """
    model = MyModel


class MyView(FormView):
    """
    Class based view taking care of rendering the form and processing it after posting. Finally, redirects you to your list view defined above.
    """
    form_class = MyForm

    def get_context_data(self, **kwargs):
        context = super(MyView, self).get_context_data(**kwargs)
        context['objects'] = MyModel.objects.all() # Customize this queryset to your liking
        return context

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.fields['checkboxes'].queryset = 
            MyModel.objects.all() # Customize this queryset to determine for which objects you want to display checkboxes
        return form

    def form_valid(self, form):
        qs = myModel.objects.filter(
            pk__in=list(map(int, self.request.POST.getlist('checkboxes'))))
        qs.delete()

        return HttpResponseRedirect(reverse_lazy('someurl'))

您的模板将如下所示(根据您发布的内容进行修改)

# template.html
{% for j in objects %}
    <h4>
        Select All
        <input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" />
    </h4>
    <button class="btn btn-danger" action=" ">Delete</button>
    <div class="col">
        {% if i.placename == j.gallery_place %}
            <div class="show-image">
                <img src="{{j.gallery_image.url}}" style="height:130px; 
width:130px;" />

            <tag class="one" style="margin:8%">
                <div class="form-checkbox"><input type="checkbox" name="checkboxes" value="{{ j.pk }}">
            </tag>

        </div>
        {% endif %}
    </div>
{% endfor %}

最后,连接网址。

# urls.py
from django.conf.urls import include, url

from.views import MyView, MyListView

urlpatterns = [
    url(r'^listdelete/$', MyView.as_view(), name="delete-list"),
    url(r'^mylist/$, MyListView.as_view(), name="someurl"),
]