我想在没有Django REST框架的情况下实现它。
这是我的代码:
from django.http import JsonResponse
def index1(request):
cursor.execute('select * from users where id = %s', [request.POST])
response = list(cursor.fetchall())
return JsonResponse(response, safe=False)
当我使用flutter HTTP POST时,它会给我这个
Forbidden (CSRF cookie not set.): /index1
[04/Jun/2021 00:20:25] "POST /index1 HTTP/1.1" 403 2870
如果您不想验证CSRF(通常只有当这是API时(
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def index1(request):
cursor.execute('select * from users where id = %s',[request.POST])
response = list(cursor.fetchall())
return JsonResponse(response, safe=False)
请参阅此处的文档https://docs.djangoproject.com/en/3.2/ref/csrf/#django.views.decorators.csrf.csrf_exempt