1

I am using this library for datatables in django-rest. Everything is working fine expect request.user session in views. It seems to me django-datatable is not authenticating the user token and therefore request.user returns anonymous 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

2 Answers 2

1

Have You tried to subclass BaseDatatableView and overwrite its .get like:

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. When You look into the code of django_datatables/mixins.py, there is a mixin called JsonResponseMixin. It's GET method is directly responsible for request processing, so You should look for Your answers there. The easiest way - subclass it and overwrite the method.

Sign up to request clarification or add additional context in comments.

5 Comments

No , I haven't overwrite get , but surely will look at this
Are You sending the token with the request?
Have You tried to check if Authentication header is present in the request? You can check it like "Authentication" in self.request
sorry for late reply , I checked for the Authentication header and its not there in request but I am sending the Authentication header like for other requests -> response = requests.get(api, headers={'Authorization': token_header}, params=request.GET.dict(), verify=True) .
also token_header is present here self.request.META['HTTP_AUTHORIZATION']
0

Have you added the token JS to the Datatables initiation JS file? django-datatables just creates the correct JSON string. Initiating the cookie is different.

I fought with this a while and my missing piece was that I had to get and set the 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": {

........

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.