Django rest framework - Authentication error with PUT requests

前端 未结 3 702
迷失自我
迷失自我 2021-02-11 03:44

I have a very simple Resource like this for my model \'Presentacion\'

class PresentacionResource(ModelResource):
    m         


        
3条回答
  •  一个人的身影
    2021-02-11 04:40

    If the issue is the X-CSRF token header you can modify the Backbone.sync like this to send a token with each POST, PUT, DELETE request.

            /* alias away the sync method */
            Backbone._sync = Backbone.sync;
    
            /* define a new sync method */
            Backbone.sync = function(method, model, options) {
    
                /* only need a token for non-get requests */
                if (method == 'create' || method == 'update' || method == 'delete') {
                    // CSRF token value is in an embedded meta tag 
                    var csrfToken = $("meta[name='csrf_token']").attr('content');
    
                    options.beforeSend = function(xhr){
                        xhr.setRequestHeader('X-CSRFToken', csrfToken);
                    };
                }
    
                /* proxy the call to the old sync method */
                return Backbone._sync(method, model, options);
            };
    

提交回复
热议问题