django在没有csrf令牌的情况下工作时的curl请求

django在没有csrf令牌的情况下工作时的curl请求,django,django-csrf,Django,Django Csrf,我在Heroku上运行了一个django项目,使用django REST框架 我使用以下中间件: MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middlewa

我在Heroku上运行了一个django项目,使用django REST框架 我使用以下中间件:

    MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
以下是我的一个基于类的视图:

class CommunityMemberView(APIView):
    permissions_classes = [IsAuthenticated, ]
    serializer_class = CommunitySerializer

    def post(self, request, community_id=None):
    """
    add the member identified by 'id'
    to the community 'community_id', if it exists.
    """
    data = request.data
    id = data.get('id')
    account = Account.objects.get(id=id)
    community = Community.objects.get(id=community_id)
    community.add_member(account)
    serializer = self.serializer_class(community, context={'request': request})

    return Response(serializer.data, status=status.HTTP_201_CREATED)
当我尝试使用curl执行POST请求时,如果没有任何csrf令牌,它工作得很好,我不知道为什么。我不使用任何装饰器,但如果我正确理解django文档,我就不需要这样做

 curl -v -H "content:application/json" -X POST -H "Content-Type:application/json" -d '{"id":"3"}' https://www.example.com/api/v1/community/2/member/ | python -m json.tool
我猜有一个明显的原因,但我在django文档和堆栈溢出中都找不到,之前所有关于相关主题的问题都是关于为什么它不起作用,而不是相反


谢谢

我假设您使用的是
django rest框架

DRF视图是用/
@csrf\u excempt
decorator包装的,除非您使用的是
SessionAuthentication


请参见

假设您使用的是
django rest框架

DRF视图是用/
@csrf\u excempt
decorator包装的,除非您使用的是
SessionAuthentication


请参见

谢谢你的回答,是的,我使用django rest框架,现在没有使用SessionAuthentication,所以你一定是对的。谢谢你的回答,是的,我使用django rest框架,现在没有使用SessionAuthentication,所以你一定是对的。