代码之家  ›  专栏  ›  技术社区  ›  Sadman Sobhan

如何为Django DeleteView使用bootbox?

  •  2
  • Sadman Sobhan  · 技术社区  · 6 年前

    我使用Django DeleteView删除对象。首先,我实现了删除添加确认对话框重定向另一个html页面。现在,我想添加bootbox弹出窗口。但我不知道在哪里添加代码。请帮忙

    模型。py公司

    class Review(models.Model):
      review_description = models.TextField(max_length=500)
      user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
      book = models.ForeignKey(Book, on_delete=models.CASCADE)
      updated = models.DateTimeField(auto_now=True, auto_now_add=False)
      timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    

    意见。py公司

    class ReviewDelete(DeleteView):
      model = Review
      template_name = "confirm_delete.html"
    
      def get_success_url(self, *args, **kwargs):
        review = get_object_or_404(Review, pk=self.kwargs['pk'])
        return reverse("books:detail", args = (review.book.id,))
    

    确认删除。html

    {% extends "base.html" %}
    {% block content %}
    <h1>Delete</h1>
    <p>Are you sure you want to delete {{ review }}?</p>
    <form action="{% url "delete" pk=review.id %}" method="POST">
      {% csrf_token %}
      <input type="submit" value="Yes, delete." />
      <a href="{% url "books:detail"  id=review.book.id %}">No, cancel.</a>
    </form>
    {% endblock %}
    

    book_详情。html

    <a href="{% url "delete" pk=review.id %}" class="badge badge-danger">Delete</a>
    {# <a href="{% url "delete" pk=review.id %}" class="badge badge-danger">Delete</a>#}
    

    基础html

    <script type="text/javascript">
        $(document).ready(function () {
            $("#review-delete-btn").click(function (event) {
                event.preventDefault();
                bootbox.confirm({
                    title: "Destroy planet?",
                    message: "Do you want to delete? This cannot be undone.",
                    buttons: {
                        cancel: {
                            label: '<i class="fa fa-times"></i> Cancel'
                        },
                        confirm: {
                            label: '<i class="fa fa-check"></i> Confirm'
                        }
                    },
                    callback: function (result) {
                        console.log('This was logged in the callback: ' + result);
                    }
                });
            });
        });
    </script>
    

    URL。py公司

    url(r'^reviews/(?P<pk>\d+)/delete/$', ReviewDelete.as_view(), name='delete'),
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Tieson T.    6 年前

    假设您的服务器页面在完成删除时不执行重定向,您只需要向确认回调添加一个AJAX调用。类似这样:

    <script>
        $(function () {
            $("#review-delete-btn").on('click', function (event) {
                event.preventDefault();
    
                // for referencing this later in this function
                var _button = $(this);
    
                bootbox.confirm({
                    title: "Destroy planet?",
                    message: "Do you want to delete? This cannot be undone.",
                    buttons: {
                        cancel: {
                            label: '<i class="fa fa-times"></i> Cancel'
                        },
                        confirm: {
                            label: '<i class="fa fa-check"></i> Confirm'
                        }
                    },
                    callback: function (result) {
                        // result will be a Boolean value
                        if(result){
                            // this encodes the form data
                            var data = _button.closest('form').serialize();
    
                            $.post('your delete URL here', data).
                                done(function(response, status, jqxhr){
                                    // status code 200 response
                                })
                                .fail(function(jqxhr, status, errorThrown){
                                    // any other status code, including 30x (redirects)
                                });
                        }
                    }
                });
            });
        });
    </script>
    

    您可能需要查看以下文档: $.post serialize . 我还建议您通过 AJAX 主题-$。post是一种方便的方法。ajax,所以您应该知道如何同时使用这两种技术。