简体   繁体   English

Django Rest 框架 - 为什么我得到的 CSRF cookie 不是只设置在一个 URL 上,而与另一个 forms 没有区别

[英]Django Rest Framework - Why am I getting a CSRF cookie not set on only one URL when there is NO difference from the other forms

I have this URL for someone to rate an opportunity:我有这个 URL 供某人评价机会:

path("opportunities/rate/", RateOpportunity.as_view),

I am using a Vue application to make a post request and all the other forms on the site work fine and I can make the requests and post content but this specific endpoint gives me a CSRF cookie not set error.我正在使用 Vue 应用程序发出发布请求,网站上的所有其他 forms 工作正常,我可以发出请求并发布内容,但这个特定端点给我一个 CSRF cookie not set 错误。 There is no difference between this form and the other forms.这个表格和其他forms没有区别。

This is the view I am using:这是我正在使用的视图:

class RateOpportunity(generics.CreateAPIView):
    permission_classes = [permissions.IsAuthenticated]

    serializer_class = OpportunityRateSerializer

    name = "rate-opportunity"

    def get_queryset(self):
        id = self.kwargs["pk"]

        return Opportunity.objects.all().filter(opportunity=id)

Why am I getting this error only on this endpoint and not any one of the others?为什么我只在这个端点上收到这个错误,而在其他任何一个端点上都没有?

first of all - you need to call method as_view in your path.首先 - 你需要在你的路径中调用方法as_view

secondly.其次。 As I know CreateAPIView do not using get_queryset method at all, so you can remove it.据我所知CreateAPIView根本不使用get_queryset方法,因此您可以将其删除。

In your frontend framework when you send request to backend - do you add something like withCredentials = true;在您的前端框架中,当您向后端发送请求时 - 您是否添加了类似withCredentials = true; . . idk I am not frontender. idk我不是前端。

But cookie is only browser feature, so make sure that cookie is setted但是cookie只是浏览器的特性,所以一定要设置cookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM