[英]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.id
和item_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.