Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
deleteView中的Django AJAX删除对象_Django_Ajax_Django Class Based Views - Fatal编程技术网

deleteView中的Django AJAX删除对象

deleteView中的Django AJAX删除对象,django,ajax,django-class-based-views,Django,Ajax,Django Class Based Views,你好 我试图通过删除一个对象来理解如何使用ajax 主要问题是,我不明白如何在AJAX调用中将slug参数传递给url path('posts//delete/',DeletePost.as_view(),name='delete_post') 我还想了解的是,功能是否正确?我在没有json的情况下进行了测试,get\u object返回正确的对象 {% for post in user_posts %} <tr id="post

你好

我试图通过删除一个对象来理解如何使用ajax

主要问题是,我不明白如何在
AJAX
调用中将slug参数传递给url

path('posts//delete/',DeletePost.as_view(),name='delete_post')

我还想了解的是,功能是否正确?我在没有json的情况下进行了测试,
get\u object
返回正确的对象

  {% for post in user_posts %}
                            <tr id="post_table">
                                <th scope="row">
                                    <input type="checkbox" aria-label="Checkbox" style="display: none">
                                </th>
                                <td class="tm-product-name"><a href="{% url 'post:edit_post' post.slug %}">{{ post.title }}</a></td>
                                    <td class="text-center">145</td>
                                    <td class="text-center">{{ post.total_likes }}</td>
                                    <td>{{ post.created_at }}</td>
                                    <td><button class="delete-icon" onclick='deletePost({{ post.slug }})'>btn</button></td>
                                    {#<td><i  class="fas fa-trash-alt tm-trash-icon delete-icon" id="{{ post.id }}"></i></td>#}
                                    <td>
                                    {#<form method="post" action="{% url 'post:delete_post' post.slug %}" id="delete">#}
                                        {#{% csrf_token %}#}
                                        {#<i  class="fas fa-trash-alt tm-trash-icon"></i>#}
                                    {#</form>#}
                                    </td>
                            </tr>
                        {% endfor %}


<script>
        function deletePost(slug) {
            let action = confirm('Are you sure you want to delete this post?');
            if (action !== false) {
                $.ajax({
                    method: 'POST',
                    url: '{% url 'post:delete_post'%}',
                    success: function (data) {
                        if (data.delete()) {
                            $('#post_table').remove()
                        }
                    }
                });
            }
        }

    </script>

从技术上讲,我只需要一个
ping
到正确的
DeleteView url
和一个正确的
slug
,是否需要在
DeleteView
中解析
json

将我的
视图更改为

class DeletePost(CsrfExemptMixin, SingleObjectMixin, View):
    model = Post

    def post(self, *args, **kwargs):
        self.object = self.get_object()
        self.object.delete()
        data = {'success': 'OK'}
        return JsonResponse(data) 
并修改了ajax
请求

var slug = document.getElementById('delete-icon').getAttribute('data-id');
        function deletePost() {
            let action = confirm('Are you sure you want to delete this post?');

            let url = '/posts/' + slug + '/delete/';
            if (action != false) {
                $.ajax({
                    method: 'POST',
                    url: url,
                    dataType: 'json',
                    success: function (data) {
                        $('#post_table').remove()
                    }
                });
            }
        }

无论如何,我找到了解决办法

将我的
视图更改为

class DeletePost(CsrfExemptMixin, SingleObjectMixin, View):
    model = Post

    def post(self, *args, **kwargs):
        self.object = self.get_object()
        self.object.delete()
        data = {'success': 'OK'}
        return JsonResponse(data) 
并修改了ajax
请求

var slug = document.getElementById('delete-icon').getAttribute('data-id');
        function deletePost() {
            let action = confirm('Are you sure you want to delete this post?');

            let url = '/posts/' + slug + '/delete/';
            if (action != false) {
                $.ajax({
                    method: 'POST',
                    url: url,
                    dataType: 'json',
                    success: function (data) {
                        $('#post_table').remove()
                    }
                });
            }
        }

我相信你可以从[code>self.kwargsdict获得
slug
,类似这样的:`slug=self.kwargs.get('slug')。我相信你可以从
self.kwargs
dict获得
slug
,类似这样的:`slug=self.kwargs.get('slug')。