2012-09-08 59 views
2

我有一个非常简单的资源这样对我的模式“PRESENTACION”Django的REST框架 - 与PUT验证错误请求

class PresentacionResource(ModelResource): 
    model = Presentacion 
    fields = (some fields) 
    ignore_fields = (few to ignore) 

,我需要完成这个认证,所以只要我阅读,我创建了两个包装

class AuthListOrCreateModelView(ListOrCreateModelView): 
    permissions = (IsAuthenticated,) 
class AuthInstanceModelView(InstanceModelView): 
    permissions = (IsAuthenticated,) 

然后在我在我的urls.py

url(r'^presentaciones/$', AuthListOrCreateModelView.as_view(resource=PresentacionResource), name='presentacion-root'), 
url(r'^presentaciones/(?P<id>[0-9]+)$', AuthInstanceModelView.as_view(resource=PresentacionResource), name='presentacion'), 

这是工作网络ne为GET'presentaciones /'请求,但是当我尝试做一个PUT请求时,我得到一个403 FORBIDDEN

我奇怪的是GET工作正常:只要我登录,它就是正确响应,但如果我注销它回应403 FORBIDDEN。

回答

3

如果您使用的是基于Django的基于会话的身份验证,那么您可能会跳过构建到Django中的CSRF保护(请参阅UserLoggedInAuthentication类[1])。

如果是这种情况,您需要确保CSRF cookie发送到客户端,然后您可以调整jQuery指令[2]以发送可能会更改数据的请求的X-CSRFToken标头。

[1] http://django-rest-framework.org/_modules/authentication.html

[2] https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

9

如果问题是X CSRF令牌头可以修改Backbone.sync这样发送的令牌与每个POST,PUT,DELETE请求。

 /* 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); 
     }; 
2

我意识到这是一个较旧的帖子,但我最近正在处理这个问题。扩展@orangewarp的答案和使用django文档(https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax),这里有一个解决方案:

此解决方案使用csrftoken cookie。另一个解决方案是在你的API中创建一个csrf标记端点并从那里获取csrf。

Backbone._sync = Backbone.sync; 

    Backbone.sync = function(method, model, options) { 
     //from django docs 
     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; 
     } 

     /* only need a token for non-get requests */ 
     if (method == 'create' || method == 'update' || method == 'delete') { 
      var csrfToken = getCookie('csrftoken'); 

      options.beforeSend = function(xhr){ 
       xhr.setRequestHeader('X-CSRFToken', csrfToken); 
      }; 
     } 

     return Backbone._sync(method, model, options); 
    };