为什么发布请求数据在Django应用中不可用?

3mpgtkmj  于 2023-05-19  发布在  Go
关注(0)|答案(2)|浏览(136)

我在下面的代码中使用dio从flutter应用程序发送了一个帖子请求

Map<String, String> getData() {
    return {
      "firstname": firstname.value,
      "lastname": lastname.value,
      "phonenumber": phoneNumber.value,
      "password": password.value,
      "accounttype": accounttype.value
    };
  }

  void signup(context) async {
    Dio dio = Dio();
    final response = await dio.post(
      'http://192.168.0.101:8000/signup/',
      data: getData(),
    );
    debugPrint(response.toString());
  }

并尝试在django中从下面的代码打印它

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def signup(request):
    if request.method == 'POST':
        print(request.POST)
        return HttpResponse('Hello, world!')

印刷时我得到一本空字典。

afdcj2ne

afdcj2ne1#

因此,而不是从request.POST访问数据,您需要从www.example.com访问它request.data如下面的帖子所述
Django & TastyPie: request.POST is empty

xienkqul

xienkqul2#

尝试打印类似

@csrf_exempt
def signup(request):
    if request.method == 'POST':
        print(request.data)
        return HttpResponse('Hello, world!')

相关问题