其他分享
首页 > 其他分享> > ajax传参

ajax传参

作者:互联网

{% extends 'home.html' %}
{% block contect %}
    <div class="container">
        <div class="row">

            <div class="col-md-7 col-md-offset-2">
                <a href="{% url 'book_add.view' %}" class="btn btn-success btn-xs">添加书籍</a>
                <a href="{% url 'home.view' %}" class="btn-xs btn">首页</a>
                <h1 class="text-center">数据展示</h1>
                <table class="table table-hover table-striped">
                    <thead>
                    <tr>
                        <th class="text-center">id</th>
                        <th class="text-center">title</th>
                        <th class="text-center">price</th>
                        <th class="text-center">publish</th>
                        <th class="text-center">publish_time</th>
                        <th class="text-center">author</th>
                        <th class="text-center">operation</th>
                    </tr>
                    </thead>
                    <tbody>

                    {% for book_obj in book_queryset %}
                        <tr>
                            <td class="text-center">{{ forloop.counter }}</td>
                            <td class="text-center">{{ book_obj.title }}</td>
                            <td class="text-center"> {{ book_obj.price }}</td>
                            <td class="text-center">{{ book_obj.publish.name }}</td>
                            <td class="text-center">{{ book_obj.publish_time|date:'Y-m-d' }}</td>
                            <td class="text-center">
                                {% for author_obj in book_obj.aithors.all %}
                                    {% if forloop.last %}
                                        <span>{{ author_obj.name }}</span>
                                    {% else %}
                                        <span>{{ author_obj.name }},</span>
                                    {% endif %}
                                {% endfor %}
                            </td>
                            <td><a href="{% url 'book_edit.view' book_obj.id %}" class="btn btn-primary btn-xs">编辑</a>
                                <a href="#" class="btn btn-danger btn-xs delBtn">删除</a>
                                <input type="text" id='delete_id' value="{{ book_obj.pk }}">
                            </td>
                        </tr>

                    {% endfor %}


                    </tbody>

                </table>

            </div>
        </div>

    </div>
{% endblock %}

{% block js %}

    <style>
        #delete_id {
            display: none;
        }
    </style>

    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

    <script>

        $('.delBtn').click(function () {
            swal({
                title: "Are you sure?",
                text: "Once deleted, you will not be able to recover this imaginary file!",
                icon: "warning",
                buttons: ['False','True'],
                dangerMode: true,
            })
                .then((willDelete) => {
                    let id=$('#delete_id').val()
                    if (willDelete) {$.ajax({
                            url:{% url 'delete_view' %},
                            type:'post',
                            data:{'delete_id':id},
                            success:function (args){console.log(args)}
                        })

                        swal("Poof! Your imaginary file has been deleted!", {
                            icon: "success",
                        });
                        function f(){window.location.href={% url 'book.view' %}}
                     setTimeout(f,1000)}
                    else {
                        swal("Your imaginary file is safe!");
                        return false
                    }
                });


        })

    </script>

标签:传参,obj,author,publish,ajax,book,id,delete
来源: https://www.cnblogs.com/tzmy/p/16671197.html