简体   繁体   English

Django rest框架,通过Angular发送时,序列化程序无效

[英]Django rest framework, serializer invalid when sent through Angular

I'm having a problem where when I make a post request from my Angular application served through Django I get invalid serializers, even if the output is correct. 我遇到一个问题,当我通过Django通过Angular应用程序发出发布请求时,即使输出正确,我也会得到无效的序列化程序。

I console log the sent data: 我控制台记录发送的数据:

{"email":"example@gmail.com","username":"example","password":"plaintextforthisexample","confirm_password":"plaintextforthisexample"}

Which gives me the response Bad Request (which I have set to happen when serializer.is_valid() is false ). 这给了我错误的请求响应(我设置为在serializer.is_valid()false时发生)。

However, if I copy and paste what's in the console log into the browsable API under raw data it accepts it and creates a user. 但是,如果我将控制台日志中的内容复制并粘贴到原始数据下的可浏览API中,它将接受它并创建一个用户。

I can successfully send get-requests to the same API and get the data back, and the permissions for getting and posts are the same, although that would result in another type of error. 我可以成功地将get-requests发送到相同的API并取回数据,获取和发布的权限是相同的,尽管这会导致另一种错误。 The content type is correct as well, when not I get a 415. If the URL is wrong I get a 500. And I can see on the Django server log that post requests are coming in. Could it be because I'm running it from an angular template and not a Django one? 内容类型也是正确的,如果不是,我得到415。如果URL错误,我得到500。而且我可以在Django服务器日志上看到张贴请求进入。可能是因为我正在运行它从一个有角度的模板而不是Django的模板?

EDIT: 编辑:

Picture of the console: 控制台图片: 在此处输入图片说明

Issue 1: The console logged output that worked in Django was not the one actually being sent, resulting in a JSONParse error. 问题1:在Django中工作的控制台记录的输出不是实际发送的,导致JSONParse错误。 It looked like something like this 看起来像这样

function(formobject){
let payload = JSON.stringify(formobject)
console.log(payload)

return formobject

Issue 2: Now Django gives my my custom response on arrival: 问题2:现在Django在到达时给了我我的自定义响应:

Status:'Bad Request' Message:'Data does not fullfill requirements for creating a user'

And does not allow me to copy-paste the JSON-output from the console into the API raw data-form. 并且不允许我将控制台中的JSON输出复制粘贴到API原始数据格式中。

Solved by not using the same password I had been using for about 15 tries. 通过不使用大约15次尝试使用的相同密码来解决。 Seems Django detects common passwords, has not been fully tested. 似乎Django检测到通用密码,尚未经过全面测试。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM