简体   繁体   English

如何从 jQuery 数据表调用 django 视图?

[英]How can i call django view from jQuery Datatables?

I'm using datatables and I'm trying to add some buttons with actions to them.我正在使用数据表,并且正在尝试向它们添加一些带有操作的按钮。 I want to call django view function, but I'm not sure how to achieve this inside of jQuery Datatables.我想调用 django 查看 function,但我不确定如何在 jQuery 数据表中实现这一点。 When I have django templates it's clear I can just say当我有 django 模板时,很明显我可以说

<a href="{% url 'feedback:useredit' user.id %}" class="btn text-secondary px"-0>
                                            <i class="far fa-edit fa-lg"></i>

and this redirect me to "useredit" view function in django.这将我重定向到 django 中的“useredit”视图 function。 But this don't work without the django template and inside of jQuery's Datatables.但是,如果没有 django 模板和 jQuery 的数据表内部,这将不起作用。 Can you guys give me some hint how to achieve this?你们能给我一些提示如何实现这一目标吗?

             $(document).ready(function() {
                 var data;
                 fetch("http://192.168.2.85:8000/fetchapi/")
                    .then(response => response.json())
                    .then(json => data = json)
                    .then(() => {console.log(data);
                        $('#datatable').DataTable( {
                        data:           data.employees,
                        deferRender:    true,
                        scrollY:        false,
                        scrollX:        false,
                        scrollCollapse: true,
                        scroller:       true,
                        "columns": [
                            { data: "id" }, 
                            { data: "first_name" }, 
                            { data: "last_name" },
                            { data: "email" }, 
             
                                { "data" : null, render: function ( data, type, row ) {
                                    return '<button class="btn-view" type="button">Edit</button>';
                                } },
                                { "data" : null,render: function ( data, type, row ) {
                                    return '<i class="fa fa-plus-circle" aria-hidden="true"></i>';
                                } },

                        ],
                        "order": [[1, 'asc']]

                    } )
                    
                })
                } ); 

this is the whole jQuery code, but I think I need to implement the url to my django view in this part:这是整个 jQuery 代码,但我认为我需要在这部分的 django 视图中实现 url:

                            { "data" : null, render: function ( data, type, row ) {
                                return '<button class="btn-view" type="button">Edit</button>';
                            } },
                            { "data" : null,render: function ( data, type, row ) {
                                return '<i class="fa fa-plus-circle" aria-hidden="true"></i>';
                            } },

I hope my question is understandable and clear, thanks a bunch for the time!我希望我的问题是可以理解和清楚的,非常感谢您的时间!

In my opinion a quick solution.在我看来,一个快速的解决方案。 You can define your url as variable:您可以将 url 定义为变量:

var url = "{% url 'feedback:useredit' -999 %}";

and you can replace - 999 with your disered id :你可以用你的disered id替换 - 999

var desired_url = url.replace("-999", "1");
// or
var href_title = "<a href='" + "{% url 'feedback:useredit' -999 %}".replace("-999", data.id) +"'>" + data.text + "</a>";

It is of course best to generate that in your backend method (view).当然最好在您的后端方法(视图)中生成它。

from django.http import JsonResponse

def datatable_json(request):
    ... Your code here ...
    data["useredit_url"] = reverse("feedback:useredit", args=[user.id])
    return JsonResponse(data) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM