【问题标题】:DeleteView - confirm by Popup windowsDeleteView - 通过弹出窗口确认
【发布时间】:2022-01-15 00:09:33
【问题描述】:

我想问你,如果你知道,这里有一些要点或解决方案如何通过 Popup 引导窗口使用 DeleteView 类确认删除项目。所以我不会使用template_name,而是在当前页面弹出窗口。

非常感谢你所做的一切......!!!

更准确地说 我的 index.html 页面中有这个按钮

 <button type="button" class=" ml-1 btn btn-outline-dark my-2 my-sm-0" data-toggle="modal" data-target="#exampleModal">
             In
        </button>

这里我有删除模式

            <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Are you sure to delete this?</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  </div>
                  <div class="modal-footer">
                      <a href="#" class="button btn btn-primary">Yes</a>
                         <button type="button" class="btn btn-danger" data-dismiss="modal">No</button>
                  </div>
                </div>
              </div>
            </div>
    </div>

这是我要删除的views.py

class ItemDeleteView(DeleteView):
 model = Item
 # template_name = 'index.html'
 success_url = reverse_lazy('index')
 login_url = '/accounts/login/'

 def get(self, request, *args, **kwargs):
    return self.post(request, *args, **kwargs)

我不知道该怎么办。现在...谢谢

【问题讨论】:

    标签: html django django-views


    【解决方案1】:

    最好的选择是 Javascript,但您可以重新定义 DeleteView 类的 get 方法:

    将其添加到您的课程中

    def get(self, request, *args, **kwargs):
            return self.post(request, *args, **kwargs)
    

    基本上,Deleteview 需要一个发布请求才能触发,这有点小技巧,但效果很好。

    在您的模板中将其放置在您的模态或弹出窗口中

        <form method="POST" action="{% url "your_delete_url_name" %}">
       {% csrf_token %}<input type="submit" value="DELETE">
       </form>
    

    不要忘记 URL 中的对象 ID

    【讨论】:

    • 感谢您的意见。我在我的问题中添加了带有代码示例的 mroe 描述。我会很高兴获得更多帮助。非常感谢
    • 嗯...我已经给你答案了,只需将第二个 sn-p 添加到您的模式中,并带有指向您的 DeleteView 的链接。
    猜你喜欢
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 2012-02-20
    相关资源
    最近更新 更多