1, 前端 ajax 解决
$.ajax({
data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
})
2, 后端代码解决
view.py
2.1
from django.views.decorators.csrf import ensure_csrf_cookie
@csrf_exempt def my_view(request):
from django.views.decorators.csrf import csrf_protect or from django.views.decorators.csrf import requires_csrf_token or from django.views.decorators.csrf import ensure_csrf_cookie
- from django.views.decorators.csrf import requires_csrf_token
- from django.shortcuts import render
-
- @requires_csrf_token
- def my_view(request):
- c = {}
- c.update(csrf(request))
- return HttpResponse("value", c)
文档来源 |