“ WSGIRequest”对象没有属性“ data” [英] 'WSGIRequest' object has no attribute 'data'

查看:350
本文介绍了“ WSGIRequest”对象没有属性“ data”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发布到API时遇到了麻烦,无法完全弄清错误指的是什么。如果有问题,我正在使用Django REST,并包含了追溯。

I'm having trouble with posting to my API and can't quite figure out what the error is referring to. If it matters, I'm using Django REST and included the traceback.

if (repeat == false) {
    post_data = {'User': usernameInput}
    $.ajax({
        type: 'POST',
        url: '/0/addUser',
        data: post_data,
        async: true
    })
}

class AddUser(APIView):
    def post(self, request, format = None):
        serializer = UserSerializer(data=request.data)
        if serializer.isvalid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)


Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch
  403.             response = self.handle_exception(exc)
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch
  400.             response = handler(request, *args, **kwargs)
File "/Users/rae/Desktop/112/djangotemplate/notes/views.py" in post
  23.       serializer = UserSerializer(data=request.data)
File "/Library/Python/2.7/site-packages/rest_framework/request.py" in __getattr__
  436.         return getattr(self._request, attr)


推荐答案

Django REST Framework有自己的 Request 对象,该对象包装了<$ c $ Django传入的c> HttpRequest 对象,并添加了一些其他功能(例如自定义呈现和另一个身份验证层)。如果在 Request 对象上访问了不存在的任何属性,它将自动将其代理到基础的 HttpRequest ,因此通常您不会注意到它们之间的区别。

Django REST Framework has its own Request object that wraps the HttpRequest object passed in by Django and adds some additional functionality (like custom rendering and another authentication layer). If any properties are accessed on the Request object that don't exist, it will automatically proxy it to the underlying HttpRequest, so typically you don't notice the difference.

在DRF 2.x中, Request 属性具有 DATA FILES 属性,用于存储传入的数据以及已检测到的任何文件。将它们合并到DRF 3.0中,并替换为单个 data 属性。随着DRF 3.0的发布,所有文档现在都反映了新的 Request.data 属性。

In DRF 2.x, the Request property has DATA and FILES properties that store the passed in data as well as any files which have been detected. These were combined in DRF 3.0 and replaced with a single data property. As DRF 3.0 has been released, all of the documentation now reflects the new Request.data property.

您会出现要使用Django REST Framework 2.x,但您正在尝试访问DRF 3.0中引入的新属性。由于 Request 对象中不存在该对象,因此将其代理到 HttpRequest 对象,在该对象中找不到。

You appear to be using Django REST Framework 2.x, but you are trying to access the new property introduced in DRF 3.0. Because it doesn't exist on the Request object, it is being proxied down to the HttpRequest object, where it also isn't being found.

这篇关于“ WSGIRequest”对象没有属性“ data”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆