简体   繁体   English

Django-使用Rest Framework的数据表

[英]Django - Datatables with Rest Framework

I am using this library for datatables in django-rest . 我使用这个Django的休息 的DataTable。 Everything is working fine expect request.user session in views. 一切工作正常,期望request.user会话在视图中。 It seems to me django-datatable is not authenticating the user token and therefore request.user returns anonymous user. 在我看来django-datatable无法验证用户令牌,因此request.user返回匿名用户。 And the same is accessible even without sending user token in headers. 即使没有在标头中发送用户令牌,也可以访问相同的内容。

Here is my code : 这是我的代码:

class MyDataTableView(BaseDatatableView):
    """
    """
    model = MyModel
    columns = [***columns** ]
    order_columns = [***columns**]

    def get_initial_queryset(self):
        """
        initial queryset for 
        """
        self.request.user -----> returns antonymous user 

        queryset = self.model.objects
        return queryset

Have You tried to subclass BaseDatatableView and overwrite its .get like: 您是否尝试过BaseDatatableView子类并覆盖其.get像这样:

def get(self, *args, **kwargs):
    super().get(*args, **kwargs)
    print(self.request)

My guess is that get_initial_queryset can be invoked before actual request dispatch, so the user is anonymous there. 我的猜测是,可以在实际请求分派之前调用get_initial_queryset ,因此用户在那里是匿名的。 When You look into the code of django_datatables/mixins.py , there is a mixin called JsonResponseMixin . 当您查看django_datatables/mixins.py的代码时,有一个名为JsonResponseMixin的mixin。 It's GET method is directly responsible for request processing, so You should look for Your answers there. 它的GET方法直接负责请求的处理,因此您应该在此处查找答案。 The easiest way - subclass it and overwrite the method. 最简单的方法-将其子类化并覆盖该方法。

Have you added the token JS to the Datatables initiation JS file? 您是否已将令牌JS添加到Datatables启动JS文件中? django-datatables just creates the correct JSON string. django-datatables仅创建正确的JSON字符串。 Initiating the cookie is different. 启动cookie是不同的。

I fought with this a while and my missing piece was that I had to get and set the cookie: 我为此奋斗了一段时间,而我缺少的部分是我必须获取并设置Cookie:

// using jQuery
function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
var csrftoken = getCookie('csrftoken');

function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});

this is above where I set the Datatables params for example : 这是在上面我设置数据表参数的地方,例如:

let table = $('#datatables').DataTable({
    "processing": true,
    "serverSide": true,
     stateSave: true,
    "ajax": {

........ ........

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

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