CSRF 失败:来源检查失败 - http://localhost:8000/ 与任何可信来源不匹配

CSRF Failed: Origin checking failed - http://localhost:8000/ does not match any trusted origins

请帮我解决问题。 我正在构建一个由 Django Rest Framework 和 ReactJS 组成的应用程序。我使用了 ViewSets。

我的错误: enter image description here

Demo

响应数据:

{"detail":"CSRF Failed: Origin checking failed - http://localhost:8000/ does not match any trusted origins."}

ReactApp 中的 DeleteLead 函数

 export const deleteLead = (id) => (dispatch) => {
  axios
    .delete(`/api/leads/${id}/`)
    .then((res) =>
      dispatch({
        type: DELETE_LEAD,
        payload: id,
      })
    )
    .catch((err) => {
      console.log(err);
    });
};

LeadViewSet: 来自 rest_framework 导入视图集、权限 从 .serializsers 导入 LeadSerializers 来自 leads.models 导入铅

# lead viewset
class LeadViewSet(viewsets.ModelViewSet):
    queryset = Lead.objects.all()
    # permission - bu ruxsat beruvchi
    permission_classes = [
        permissions.AllowAny # barcha uchun ruxsat
    ]
    serializer_class = LeadSerializers

LeadSerzializers:

# lead serializer
class LeadSerializers(serializers.ModelSerializer):
    class Meta:
        model=Lead
        fields="__all__"

首席模特:

class Lead(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField(max_length=100, unique=True)
    message = models.TextField(max_length=500, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

尝试像这样在设置文件中设置您的 CSRF 可信来源、允许的主机

CSRF_TRUSTED_ORIGINS = [
    'http://localhost:8000'
],
ALLOWED_HOSTS = [
    'localhost',
],
CORS_ORIGIN_WHITELIST = [
    'http://localhost:8000',
]