简体   繁体   English

POST 400:错误请求 - 使用 Django REST API 和 React

[英]POST 400: Bad request - Using Django REST API and React

I'm pretty new to web development so please forgive me in advance for my ignorance.我对网络开发还很陌生,所以请提前原谅我的无知。

I'm using React to try to post data to server endpoint managed by Django using this method:我正在使用 React 尝试使用以下方法将数据发布到由 Django 管理的服务器端点:

sendData(data) {
  const url = "http://127.0.0.1:8080/api/filtros/1/";
  const requestOptions = {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify(data)
  };
  fetch(url, requestOptions);
}

On the onClick of a NavDropdown React component:在 NavDropdown React 组件的 onClick 上:

<NavDropdown.Item
  key={item.id}
  onClick={() =>
    this.sendData({
      id: 0,
      dimension_id: dimension.id,
      item_id: item.id,
      usuario_id: 1
    })
  }
>
  {item.descripcion}
</NavDropdown.Item>

This is how I register the url on the router using Django:这是我使用 Django 在路由器上注册 url 的方式:

router.register('api/filtros/1', FiltroUsuariosViewSet, 'filtro')

My Django ModelViewSet looks like this:我的 Django ModelViewSet 看起来像这样:

class FiltroUsuariosViewSet(viewsets.ModelViewSet):

    queryset = FiltroUsuarios.objects.all()
    permission_classes = [
        permissions.AllowAny
    ]
    serializer_class = FiltroUsuariosSerializers

And my Django Serializer looks like this:我的 Django Serializer 看起来像这样:

class FiltroUsuariosSerializers (serializers.ModelSerializer):
    class Meta:
        model = FiltroUsuarios
        fields = ('id', 'dimension_id', 'item_id', 'usuario_id')

    def create(self, validated_data):
        post = FiltroUsuarios.objects.create(**validated_data)

When I click on the Component I get this: POST http://127.0.0.1:8080/api/filtros/1/ 400 (Bad Request) and apparently the error is on the fetch request.当我点击组件时,我得到这个: POST http://127.0.0.1:8080/api/filtros/1/ 400 (Bad Request)显然错误出在获取请求上。

Do you guys have any idea on whats the problem?你们有什么想法吗?

Thanks a lot in advance!非常感谢!

The best way to understand and get rid of 400 Bad Request errors when wiring Django and React, is to run Django in development mode and then fire up your browser's Network tab while sending the request.在连接 Django 和 React 时,理解和消除 400 Bad Request 错误的最好方法是在开发模式下运行 Django,然后在发送请求时启动浏览器的网络选项卡。

Switch into the Network -> Response tab and call sendData() .切换到Network -> Response选项卡并调用sendData() Since you are running on Django's development server, you will get the specific exception on your 400 Bad Request error.由于您在 Django 的开发服务器上运行,您将收到 400 Bad Request 错误的特定异常。 To simulate this, see the screenshot below and notice:要模拟这一点,请参阅下面的屏幕截图并注意:

{"user": ["Incorrect type. Expected pk value, received str."]} {"user": ["类型不正确。预期的 pk 值,收到的字符串。"]}

在此处输入图片说明

Back to your problem, you have the following in your .sendData() :回到你的问题,你的.sendData()有以下内容:

x = {
      id: 0,
      dimension_id: dimension.id,
      item_id: item.id,
      usuario_id: 1
    }

Which you then call JSON.stringify() on.然后调用JSON.stringify() If dimension.id and item_id are both integer (a reasonable assumption), then you're passing the following as a payload:如果dimension.iditem_id都是整数(合理的假设),那么您将以下内容作为有效负载传递:

JSON.stringify(x)
# returns:
"{"id":0,"dimension_id":1,"item_id":2,"usuario_id":3}"

Your Django Model for FiltroUsuarios defined these columns / fields, so you now need to check both your models and FiltroUsuariosSerializers that these are expected value / value types mapping to these columns.您的FiltroUsuarios Django 模型定义了这些列/字段,因此您现在需要检查您的模型和FiltroUsuariosSerializers ,这些是映射到这些列的预期值/值类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Django Rest Framework 的 Ajax POST 请求上的 400(错误请求) - 400 (Bad Request) on an Ajax POST request using Django Rest Framework 使用 Django Rest 框架和 DataTables 的 POST 返回 400 错误请求 - POST with Django Rest Framework and DataTables returns a 400 bad request 使用反应在 django rest 框架中获取“状态 400 错误请求” - Getting “status 400 Bad Request” in django rest framework with react Django Rest框架JWT 400错误请求 - Django Rest Framework JWT 400 Bad Request 使用序列化程序在对 Django Rest-Api 的发布请求中获取“错误请求” - Getting “Bad Request” on the post request to Django Rest-Api using serializers 为什么我收到 400 bad request 错误? 使用 Django Rest 框架作为我的 web 应用程序的后端和 React 作为前端 - Why am I getting a 400 bad request error? Using the Django Rest framework as a backend and React as a frontend for my web application 我正在尝试将 reactjs axios 与 django Z65E8800B5C68002AAD896F8888B2 框架连接起来以提出 post 请求。 但我收到 POST 400(错误请求) - i am trying to connect reactjs axios with django rest framework to make the post request. but i am getting POST 400 (Bad Request) Django Rest Framework - 可浏览的 API 表单总是返回 400 个错误的请求 - Django Rest Framework - Browsable API Form always returns 400 bad request 错误请求(400)在ubuntu和Django上使用nginx - Bad request (400) using nginx on ubuntu with Django Django和Bad Request(400) - Django and Bad Request (400)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM