Django不';t使用Ajax post检查csrf令牌

Django不';t使用Ajax post检查csrf令牌,ajax,django,django-csrf,Ajax,Django,Django Csrf,根据Django的说法,Django应该在默认情况下使用中间件启用csrf令牌验证。当我查看我的设置文件时,我确实看到了包含的中间件 然而,当我使用Ajax在没有csrf令牌的情况下执行post请求时,Django只会允许它。它是否应该返回一个错误,说明csrf令牌无效?我看到很多人提出的问题,他们无法验证他们的csrf令牌,但我无法使其失效 这是我的Ajax post函数(我使用js从输入中收集数据,并将其传递给此函数): 这是我的查看功能: function ajaxPost(url, da

根据Django的说法,Django应该在默认情况下使用中间件启用csrf令牌验证。当我查看我的设置文件时,我确实看到了包含的中间件

然而,当我使用Ajax在没有csrf令牌的情况下执行post请求时,Django只会允许它。它是否应该返回一个错误,说明csrf令牌无效?我看到很多人提出的问题,他们无法验证他们的csrf令牌,但我无法使其失效

这是我的Ajax post函数(我使用js从输入中收集数据,并将其传递给此函数):

这是我的查看功能:

function ajaxPost(url, data, success) {
fetch(url, {
    method: 'POST', // or 'PUT'
    body: JSON.stringify(data),
    headers: new Headers({
        'Content-Type': 'application/json'
    })
}).then(res => res.json())
    .then(response => {
        if (response.status !== success) {
            //errors
        }
        updateView(response);
    })
    .catch(error => console.error('Error:', error))
}
@api_view(['POST'])
# API endpoint for posting bulk properties
def bulk(request):
    new_properties = []
    if request.method == 'POST':
        for obj in request.data:
            discipline = Discipline.objects.get(pk=obj['discipline.id'])
            root_function = Function.objects.get(pk=obj['root_function'])
            new_property = Property(name=obj['name'], value=obj['value'], unit=obj['unit'],
                                    discipline_id=discipline)
        new_property.save()
        new_property.function.add(root_function)
        new_properties.append(new_property)

    new_properties = json.loads(serializers.serialize('json', new_properties))

    return JsonResponse({'status': 201, 'new_properties': new_properties})

假设
api_视图
是来自django rest框架的视图,它将禁用该视图的CSRF保护


这是因为API端点经常用于没有CSRF令牌的外部请求;在这些情况下,没有必要对其进行检查。

假设
api\u视图
是来自django rest框架的视图,它将禁用该视图的CSRF保护


这是因为API端点经常用于没有CSRF令牌的外部请求;在这种情况下,没有必要检查它。

谢谢,这很有意义!谢谢,这很有道理!