gpt4 book ai didi

javascript - HTTP 400 消息 POST 请求错误 - View 和序列化器代码设计不正确

转载 作者:行者123 更新时间:2023-12-03 02:06:52 24 4
gpt4 key购买 nike

我不断收到以下错误:

POST http://127.0.0.1:8000/api/creator_signup/ 400 (Bad Request)

每当我尝试将数据从 AngularJS 应用程序发布到 Django 后端时。

在客户端,我通过此代码发布了以下帖子 ( /image/R3M8a.jpg ):

   function registerCreator(creatorData) {
var url = envService.read('apiUrl') + "/creator_signup/";
var dataJSON = {
first_name: creatorData.firstName,
last_name: creatorData.lastName,
email: creatorData.email,
youtube_channel_username: creatorData.username,
youtube_channel_url: creatorData.url,
youtube_channel_title: creatorData.title,
youtube_channel_description: creatorData.description,
photo: creatorData.photo,
youtube_channel_start_date: creatorData.startDate,
keywords: creatorData.keywords,
no_of_subscribers: creatorData.noOfSubscribers,
no_of_videos: creatorData.noOfVideos,
no_of_views: creatorData.noOfViews,
no_of_likes: creatorData.noOfLikes,
no_of_dislikes: creatorData.noOfDislikes,
location: creatorData.location,
avg_views: creatorData.avgViews,
gender: 0,
password: creatorData.password
};
var req = {
method: 'POST',
url: url,
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
data: dataJSON
};
$log.info(req);
return ($http(req).then(handleSuccess, handleError));
}

function handleSuccess(response) {
return response.data;
}

function handleError(response) {
if (!angular.isObject(response.data) || !response.data.message) {
return($q.reject('An unknown error occurred.'));
}
return($q.reject(response.data.message));
}

在服务器端,我有:

urls.py

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^creator/$', views.CreatorList.as_view(), name="creator_list"),
url(r'^creator/(?P<pk>[^/]+)/', views.CreatorDetail.as_view(), name="creator_detail"),
url(r'^creator_signup/$', views.CreatorSignup.as_view(), name="creator_signup"),
]

urlpatterns = format_suffix_patterns(urlpatterns)

models.py

class Creator(models.Model):
"""
Creator model
"""
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
first_name = models.CharField(max_length=255, null=True, default=None)
last_name = models.CharField(max_length=255, null=True, default=None)
email = models.CharField(max_length=255, null=True, default=None)
youtube_channel_username = models.CharField(max_length=255, null=True, default=None)
youtube_channel_url = models.CharField(max_length=255, null=True, default=None)
youtube_channel_title = models.CharField(max_length=255, null=True, default=None)
youtube_channel_description = models.CharField(max_length=255, null=True, default=None)
photo = models.CharField(max_length=255, null=True, default=None)
youtube_channel_start_date = models.CharField(max_length=255, null=True, default=None)
keywords = models.CharField(max_length=255, null=True, default=None)
no_of_subscribers = models.IntegerField(default=0)
no_of_videos = models.IntegerField(default=0)
no_of_views = models.IntegerField(default=0)
no_of_likes = models.IntegerField(default=0)
no_of_dislikes = models.IntegerField(default=0)
location = models.CharField(max_length=255, null=True, default=None)
avg_views = models.IntegerField(default=0)
GENDER_CHOICE_UNSET = 0
GENDER_CHOICE_MALE = 1
GENDER_CHOICE_FEMALE = 2
GENDER_CHOICES = (
(GENDER_CHOICE_UNSET, 'Unset'),
(GENDER_CHOICE_MALE, 'Male'),
(GENDER_CHOICE_FEMALE, 'Female'),
)
gender = models.IntegerField(choices=GENDER_CHOICES, default=GENDER_CHOICE_UNSET)
password = models.CharField(max_length=255, null=True, default=None)

class Meta:
verbose_name_plural = "Creators"

def __str__(self):
return "%s %s" % (self.first_name,self.last_name)

序列化器.py

class CreatorSerializer(serializers.ModelSerializer):
"""
Class to serialize Creator objects
"""
class Meta:
model = Creator
fields = (
'first_name', 'last_name', 'email', 'youtube_channel_username', 'youtube_channel_url',
'youtube_channel_title',
'youtube_channel_description', 'photo', 'youtube_channel_start_date', 'keywords', 'no_of_subscribers',
'no_of_videos', 'no_of_views', 'no_of_dislikes', 'location', 'avg_views'
)
read_only_fields = 'id'

def create(self, validated_data):
return Creator.objects.create(**validated_data)

def update(self, instance, validated_data):
pass

class CreatorSignupSerializer(serializers.Serializer):
"""
Create Creator profile
"""
first_name = serializers.CharField(required=True, write_only=True)
last_name = serializers.CharField(required=True, write_only=True)
email = serializers.CharField(required=True, write_only=True)
youtube_channel_username = serializers.CharField(required=True, write_only=True)
youtube_channel_url = serializers.CharField(required=True, write_only=True)
youtube_channel_title = serializers.CharField(required=True, write_only=True)
youtube_channel_description = serializers.CharField(required=True, write_only=True)
photo = serializers.CharField(required=True, write_only=True)
youtube_channel_start_date = serializers.CharField(required=True, write_only=True)
keywords = serializers.CharField(required=True, write_only=True)
no_of_subscribers = serializers.IntegerField(required=True, write_only=True)
no_of_videos = serializers.IntegerField(required=True, write_only=True)
no_of_views = serializers.IntegerField(required=True, write_only=True)
no_of_likes = serializers.IntegerField(required=True, write_only=True)
no_of_dislikes = serializers.IntegerField(required=True, write_only=True)
location = serializers.CharField(required=True, write_only=True)
avg_views = serializers.IntegerField(required=True, write_only=True)

class Meta:
model = Creator
fields = (
'first_name', 'last_name', 'email', 'youtube_channel_username', 'youtube_channel_url',
'youtube_channel_title',
'youtube_channel_description', 'photo', 'youtube_channel_start_date', 'keywords', 'no_of_subscribers',
'no_of_videos', 'no_of_views', 'no_of_dislikes', 'location', 'avg_views'
)
read_only_fields = 'id'

def create(self, validated_data):
return Creator.objects.create(**validated_data)

def update(self, instance, validated_data):
pass

views.py

class CreatorList(generics.ListAPIView):
"""
List all Creators
HTTP: GET
"""
queryset = Creator.objects.all()
serializer_class = CreatorSerializer


class CreatorDetail(generics.RetrieveUpdateDestroyAPIView):
"""
List one Creator
HTTP: GET
"""
queryset = Creator.objects.all()
serializer_class = CreatorSerializer

class CreatorSignup(generics.CreateAPIView):
"""
Creator signup
HTTP POST
"""
queryset = Creator.objects.all()
serializer_class = CreatorSignupSerializer

def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
return Response(data=serializer.data, status=status.HTTP_201_CREATED)

我认为问题出在views.py和serializers.py代码中,但我不确定如何诊断它。

在客户端,所有内容都在请求之前注销,看起来没问题。

最佳答案

尝试将 'content-type': 'application/x-www-form-urlencoded' 更改为 'content-type': 'application/json' registerCreator 函数。在 Django 端,数据将在 request.body 下提供,因此您的 CreatorSignup View 应如下所示:

import json
...

class CreatorSignup(generics.CreateAPIView):
"""
Creator signup
HTTP POST
"""
queryset = Creator.objects.all()
serializer_class = CreatorSignupSerializer

def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)

def create(self, request, *args, **kwargs):
request_body_dict = json.loads(request.body)
serializer = self.get_serializer(data=request_body_dict)
serializer.is_valid(raise_exception=True)
return Response(data=serializer.data, status=status.HTTP_201_CREATED)

在发送之前还需要将 JSON 转换为字符串,因此在 RegisterCreator 函数中,将 data: dataJSON 更改为 data: JSON.stringify(dataJSON )

关于javascript - HTTP 400 消息 POST 请求错误 - View 和序列化器代码设计不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49767872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com