【问题标题】:how I can use http post flutter with django我如何在 django 中使用 http post flutter
【发布时间】:2021-08-22 00:58:29
【问题描述】:

我想在没有 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

【问题讨论】:

  • 我对django不熟悉,但我认为错误很明显。您需要提供一个 CSRF 令牌(相应的 cookie)。看看docs

标签: python django api flutter django-rest-framework


【解决方案1】:

如果您不想验证 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

【讨论】:

    猜你喜欢
    • 2021-01-26
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2018-02-17
    • 2021-03-31
    • 2020-02-09
    • 2021-07-19
    相关资源
    最近更新 更多