【问题标题】:Django/Django Rest Framework - Disable CSRFDjango/Django Rest 框架 - 禁用 CSRF
【发布时间】:2017-07-02 22:30:23
【问题描述】:

我正在寻找一种简单的方法来禁用所有CSRF 验证,以便在 Postman 中测试我的 API。

到目前为止,我已经尝试添加 @decorator csrf_exempt 没有成功。 我也尝试在应用程序中创建一个 disable.py 文件,但也没有用。

我还想为所有请求停用,所以有些方法不必在任何地方添加装饰器。这是我刚拿到的一个项目,已经投入生产,但我想先在 Postman 中编写测试,然后是 TestCases。

我所有的观点都在使用“api_generics.CRUDGeneric”,

该类的声明是:

class CRUDGeneric(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin,
                  mixins.DestroyModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet):

谢谢建议

【问题讨论】:

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


【解决方案1】:

@62009030 你应该能够做到@smarber 提到的..这也可以工作..这是添加 csrf_exempt 的遍历方式

from django.conf.urls import patterns, url
from django.views.decorators.csrf import csrf_exempt

import views

urlpatterns = patterns('',
    url('^asimpleurl/$', csrf_exempt(views.CRUDGeneric.as_view())),
    ...
)

这可以解决您的问题..

【讨论】:

    猜你喜欢
    • 2016-06-23
    • 2015-09-01
    • 2018-08-12
    • 2018-08-20
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多