发布json数据导致csrf令牌丢失或无效

时间:2017-10-31 12:00:17

标签: django django-rest-framework django-csrf

如果有人能帮助我找到问题,我将不胜感激。我知道有很多关于这个具体问题的解决方案。现在已经被困在这里很长一段时间了。

我的代码

浏览

class HolidayList(ListCreateAPIView):
    queryset = Holiday.objects.all()
    serializer_class = HolidaySerializer
    permission_classes = [IsAdminUser, IsAuthenticated]
    authentication_classes = [SessionAuthentication,BasicAuthentication]

URL

url(r'^$', HolidayList.as_view(), name='holiday-list-api'),

获取此错误

{"detail":"CSRF Failed: CSRF token missing or incorrect."}

我的休息框架配置

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'base.csrf_exempt.CsrfExemptSessionAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
        'rest_framework.permissions.IsAdminUser',
    ),
}

试图使用CsrfExempt但没有运气。我在这里缺少什么?

from rest_framework.authentication import SessionAuthentication

class CsrfExemptSessionAuthentication(SessionAuthentication):
   def enforce_csrf(self, request):
     return

1 个答案:

答案 0 :(得分:1)

遇到同样的问题并使用以下jquery片段解决

 Date       ID  a   b   c   d   e   f   g   h        i               j          y
 01/01/2017 1   1   0   0   1   0   0   1   6.5 -0.287199892    6.26048245  0.380978369
 01/01/2017 2   0   0   0   1   0   0   1   6.5 -0.287199892    6.26048245  0.380978369
 01/01/2017 3   1   0   0   0   1   0   0   7.8 -0.287199892    6.26048245  0.524437496
 01/03/2017 4   1   0   0   0   0   0   0   7.8 -0.260937218    6.258402008 0.63409868
 01/04/2017 5   0   0   0   1   0   0   1   6.5 10.51545939 6.263858877 0.392317155
 01/04/2017 6   0   0   0   1   0   0   1   6.5 10.51545939 6.263858877 0.392317155
 01/04/2017 7   0   1   0   1   0   0   0   6.5 10.51545939 6.263858877 1.049993284
 01/04/2017 8   0   0   0   0   1   0   0   7.3 10.51545939 6.263858877 0.461989851
 01/05/2017 9   0   0   0   0   1   0   0   6.1 -16.12973095    6.280696169 0.69686996
 01/05/2017 10  0   0   0   1   0   0   0   7.7 -16.12973095    6.280696169 0.639270495
 01/05/2017 11  0   0   0   0   1   0   0   7.3 -16.12973095    6.280696169 0.369339223
 01/06/2017 12  1   0   0   1   0   0   1   6.5 -7.097505117    6.281526986 0.395179169
 01/06/2017 13  0   1   0   1   0   0   0   6.3 -7.097505117    6.281526986 0.634524509
 01/06/2017 14  0   1   0   1   0   0   0   7.8 -7.097505117    6.281526986 0.605731699
 01/06/2017 15  0   0   0   0   0   0   0   3.2 -7.097505117    6.281526986 1.765103139
 01/07/2017 16  0   1   0   1   0   0   1   6.5 -7.097505117    6.281526986 0.323052418
 01/07/2017 17  0   0   0   1   0   0   1   6.5 -7.097505117    6.281526986 0.323052418
 01/08/2017 18  0   0   0   1   0   0   1   6.5 -7.097505117    6.281526986 0.357581409
 01/09/2017 19  0   0   0   1   0   0   1   6.5 -0.376295821    6.278540118 0.375177221
 01/09/2017 20  0   0   0   1   0   0   1   6.5 -0.376295821    6.278540118 0.375177221
 01/10/2017 21  0   0   0   1   0   0   1   6.5 1.07381926  6.275634353 0.323677822
 01/10/2017 22  1   0   0   0   0   0   0   6.3 1.07381926  6.275634353 0.529304377
 01/11/2017 23  0   0   0   1   0   0   1   6.5 -15.99695552    6.292042205 0.272404556
 01/11/2017 24  0   0   0   1   0   0   1   6.5 -15.99695552    6.292042205 0.272404556
 01/11/2017 25  0   0   0   1   0   0   0   5.8 -15.99695552    6.292042205 0.485387413
 01/11/2017 26  0   0   0   0   1   0   0   6.3 -15.99695552    6.292042205 0.651151817
 01/12/2017 27  0   1   0   1   0   0   1   6.5 4.672168917 6.290699191 0.259498815
 01/12/2017 28  0   1   0   1   0   0   0   7.3 4.672168917 6.290699191 0.396883681
 01/13/2017 29  0   0   0   1   0   0   1   6.5 2.818656098 6.288309121 0.247276795
 01/13/2017 30  0   0   0   1   0   0   0   6.1 2.818656098 6.288309121 0.72878018
 01/13/2017 31  1   0   0   0   0   0   0   6.3 2.818656098 6.288309121 0.439525331
 01/13/2017 32  1   0   0   0   0   0   0   6.3 2.818656098 6.288309121 0.439525331
 01/13/2017 33  0   0   0   1   0   0   0   7.8 2.818656098 6.288309121 0.674418975
 01/14/2017 34  0   0   0   1   0   0   1   6.5 2.818656098 6.288309121 0.228731465
 01/14/2017 35  0   0   0   1   0   0   1   6.5 2.818656098 6.288309121 0.228731465
 01/14/2017 36  1   0   0   0   0   0   0   3.2 2.818656098 6.288309121 1.614602435
 01/15/2017 37  0   1   0   1   0   0   1   6.5 2.818656098 6.288309121 0.247426893
 01/15/2017 38  0   0   1   1   0   0   0   7.3 2.818656098 6.288309121 0.557578826
 01/16/2017 39  0   0   0   1   0   0   1   6.5 0.393759618 6.285334223 0.27193064
 01/16/2017 40  0   0   0   0   1   0   0   7.3 0.393759618 6.285334223 0.85500972
 01/16/2017 41  0   1   0   1   0   0   0   5.7 0.393759618 6.285334223 0.676041272
 01/17/2017 42  0   1   0   1   0   0   1   6.5 7.88986693  6.287050627 0.271868581
 01/17/2017 43  0   1   0   1   0   0   0   5.7 7.88986693  6.287050627 0.601684565
 01/17/2017 44  0   0   0   1   0   0   0   7.3 7.88986693  6.287050627 0.561442547
 01/17/2017 45  0   0   0   1   0   0   0   5.7 7.88986693  6.287050627 0.426279123
 01/18/2017 46  0   0   0   1   0   0   1   6.5 -2.816167339    6.284668375 0.247257528
 01/18/2017 47  0   1   0   1   0   0   1   6.5 -2.816167339    6.284668375 0.247257528
 01/18/2017 48  0   1   0   1   0   0   1   6.5 -2.816167339    6.284668375 0.247257528
 01/20/2017 49  0   0   0   1   0   0   1   6.5 -0.673663504    6.279474132 0.240110499
 01/20/2017 50  0   0   0   1   0   0   1   6.5 -0.673663504    6.279474132 0.240110499
 01/20/2017 51  0   1   0   1   0   0   1   6.5 -0.673663504    6.279474132 0.240110499
 01/20/2017 52  0   0   0   1   0   0   1   6.5 -0.673663504    6.279474132 0.240110499
 01/21/2017 53  0   1   0   1   0   0   1   6.5 -0.673663504    6.279474132 0.255335931
 01/21/2017 54  0   0   0   1   0   0   0   7.3 -0.673663504    6.279474132 0.511621616
 01/21/2017 55  0   1   0   1   0   0   0   7.8 -0.673663504    6.279474132 0.658433144
 01/22/2017 56  0   1   0   1   0   0   1   6.5 -0.673663504    6.279474132 0.295583923
 01/23/2017 57  0   0   0   1   0   0   0   6.5 2.407225453 6.276942114 1.536492494
 01/23/2017 58  0   0   0   0   0   1   0   5.2 2.407225453 6.276942114 0.484052447
 01/23/2017 59  0   0   1   1   0   0   0   7.8 2.407225453 6.276942114 0.781409215
 01/24/2017 60  0   1   0   1   0   0   1   6.5 -1.218354632    6.274089502 0.321647225
 01/24/2017 61  0   0   0   1   0   0   1   6.5 -1.218354632    6.274089502 0.321647225
 01/25/2017 62  0   0   0   1   0   0   1   6.5 -1.851624795    6.271387207 0.307303763
 01/25/2017 63  0   0   0   1   0   0   1   6.5 -1.851624795    6.271387207 0.307303763
 01/25/2017 64  0   1   0   1   0   0   1   6.5 -1.851624795    6.271387207 0.307303763
 01/25/2017 65  0   0   0   1   0   0   1   6.5 -1.851624795    6.271387207 0.307303763
 01/25/2017 66  0   0   0   1   0   0   1   6.5 -1.851624795    6.271387207 0.307303763
 01/25/2017 67  0   1   0   1   0   0   0   4.3 -1.851624795    6.271387207 0.613050528
 01/25/2017 68  0   1   0   1   0   0   0   7.3 -1.851624795    6.271387207 0.546333861
 01/25/2017 69  0   0   0   0   1   0   0   7.2 -1.851624795    6.271387207 0.537850062
 01/25/2017 70  0   1   0   0   1   0   0   7.8 -1.851624795    6.271387207 0.639599631
 01/26/2017 71  0   0   0   1   0   0   1   6.5 2.534464661 6.26891399  0.300565872
 01/26/2017 72  0   0   0   1   0   0   1   6.5 2.534464661 6.26891399  0.300565872
 01/26/2017 73  0   1   0   1   0   0   0   8.3 2.534464661 6.26891399  0.529403228
 01/27/2017 74  0   0   0   1   0   0   1   6.5 0.448048274 6.265976913 0.300919416
 01/27/2017 75  0   1   0   1   0   0   1   6.5 0.448048274 6.265976913 0.300919416
 01/27/2017 76  1   0   0   1   0   0   0   5.7 0.448048274 6.265976913 0.580776132
 01/27/2017 77  0   0   0   0   1   0   0   6.1 0.448048274 6.265976913 0.686217699
 02/01/2017 78  0   0   1   1   0   0   0   6.5 1.735858826 6.258495118 1.134076442
 02/02/2017 79  1   0   0   1   0   0   1   6.5 2.678162264 6.256096701 0.246605523
 02/02/2017 80  0   1   0   0   1   0   0   5.7 2.678162264 6.256096701 0.788278101
 02/03/2017 81  0   0   0   1   0   0   1   6.5 1.153969121 6.253264176 0.240588472
 02/03/2017 82  1   0   0   0   1   0   0   7.2 1.153969121 6.253264176 0.504511317
 02/03/2017 83  1   0   0   1   0   0   0   7.8 1.153969121 6.253264176 0.367633571
 02/04/2017 84  0   0   0   1   0   0   1   6.5 1.153969121 6.253264176 0.265418597
 02/04/2017 85  0   1   0   1   0   0   0   6.1 1.153969121 6.253264176 0.657809188
 02/04/2017 86  0   1   0   1   0   0   0   7.8 1.153969121 6.253264176 0.363212341
 02/05/2017 87  0   0   0   1   0   0   0   7.3 1.153969121 6.253264176 0.508102373
 02/06/2017 88  0   0   0   0   0   0   1   6.5 1.046748087 6.250418001 0.372827574
 02/06/2017 89  0   0   0   1   0   0   1   6.5 1.046748087 6.250418001 0.372827574
 02/06/2017 90  0   1   0   1   0   0   1   6.5 1.046748087 6.250418001 0.372827574
 02/06/2017 91  0   0   0   1   0   0   0   6.1 1.046748087 6.250418001 0.755692532
 02/07/2017 92  1   0   0   0   1   0   0   6.1 2.572012207 6.247989068 0.669478955
 02/07/2017 93  1   0   0   0   0   0   0   6.3 2.572012207 6.247989068 0.533384869
 02/07/2017 94  0   0   0   1   0   0   0   5.7 2.572012207 6.247989068 0.551575392
 02/07/2017 95  0   1   0   1   0   0   0   7.7 2.572012207 6.247989068 0.544694026
 02/07/2017 96  1   0   0   0   0   0   0   7.3 2.572012207 6.247989068 0.466169871
 02/07/2017 97  0   0   0   0   0   0   0   7.8 2.572012207 6.247989068 0.352763774
 02/08/2017 98  0   1   0   1   0   0   1   6.5 0.93372207  6.245134    0.317670958
 02/08/2017 99  0   1   0   1   0   0   1   6.5 0.93372207  6.245134    0.317670958
 02/08/2017 100 0   0   0   1   0   0   0   6.1 0.93372207  6.245134    0.590983019
 02/08/2017 101 0   1   0   1   0   0   0   7.8 0.93372207  6.245134    0.288007254
 02/08/2017 102 0   0   0   1   0   0   0   7.8 0.93372207  6.245134    0.288007254
 02/09/2017 103 0   1   0   1   0   0   1   6.5 -7.364293184    6.246272494 0.322058201
 02/09/2017 104 0   1   0   1   0   0   0   7.7 -7.364293184    6.246272494 0.45710099
 02/09/2017 105 0   1   0   1   0   0   0   7.3 -7.364293184    6.246272494 0.618448667
 02/09/2017 106 1   0   0   0   1   0   0   6.7 -7.364293184    6.246272494 0.75533239
 02/10/2017 107 1   0   0   1   0   0   1   6.5 2.054261996 6.243673693 0.322091275
 02/10/2017 108 0   1   0   0   1   0   0   7.3 2.054261996 6.243673693 0.619229315
 02/10/2017 109 1   0   0   0   1   0   0   6.3 2.054261996 6.243673693 0.574019377
 02/11/2017 110 0   0   0   0   1   0   0   5.8 2.054261996 6.243673693 0.576474297
 02/12/2017 111 1   0   0   0   0   1   0   7.8 2.054261996 6.243673693 0.292138684
 02/13/2017 112 0   0   1   1   0   0   0   6.1 -0.5847667  6.2407891   0.796668232
 02/14/2017 113 1   0   0   1   0   0   1   6.5 1.418813574 6.238033264 0.347653035
 02/14/2017 114 0   0   0   1   0   0   1   6.5 1.418813574 6.238033264 0.347653035
 02/14/2017 115 0   0   0   1   0   0   1   6.5 1.418813574 6.238033264 0.347653035
 02/14/2017 116 0   0   0   0   1   0   0   6.1 1.418813574 6.238033264 0.735497343
 02/14/2017 117 0   1   0   1   0   0   0   4.3 1.418813574 6.238033264 0.51068871
 02/14/2017 118 0   0   0   0   0   1   0   6.5 1.418813574 6.238033264 0.458087049
 02/14/2017 119 0   0   0   0   1   0   0   4.5 1.418813574 6.238033264 0.402129607
 02/15/2017 120 0   1   0   0   1   0   1   6.5 -0.041897715    6.235131309 0.308459288

csrf_token存储在名为“csrftoken”的cookie中,您需要将其传递给请求标头。在您的情况下,您的POST请求标题。

希望它会有所帮助。