我在Django中有一个用于保存用户个人资料详细信息的模型,例如:
class UserDetails(models.Model):
def getFileName(self, filename):
return 'profile_pics/'+str(self.user.id)+'/'+filename
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
profile_picture = models.ImageField(blank = True, upload_to=getFileName)
country = models.CharField(max_length = 50, default='South Africa')
gender = models.CharField(max_length=10, default='NA')
birthday = models.DateField(default=datetime.now())
phone = models.CharField(max_length=15)
verified = models.BooleanField(default=False)
def __str__(self):
return self.user.username
然后,我编写了一个REST API,该API可以处理POST请求以创建用户个人资料,例如:
from django.shortcuts import render
from rest_framework import status
from rest_framework.decorators import api_view, authentication_classes, permission_classes, parser_classes
from rest_framework.parsers import FormParser, MultiPartParser, FileUploadParser
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from datetime import datetime
from django.contrib import messages, auth
from django.contrib.auth.models import User
from userMgmt.models import UserDetails
@api_view(['POST'])
@parser_classes(['FormParser', 'MultiPartParser', 'FileUploadParser'])
def signUp(request):
if request.method == 'POST':
data = request.data
## Creating a basic user
user = User.objects.create_user(data['first_name'], data['email'], data['password'])
user['last_name'] = data['last_name']
user.save()
## Creating a profile for the user
user_details = UserDetails()
user_details.user = user
user_details.profile_picture = data['profile_picture']
user_details.country = data['country']
user_details.gender = data['gender']
user_details.birthday = datetime.strptime(data['birthday'], '%m/%d/%y')
user_details.phone = data['phone']
user_details.verified = False
user_details.save()
return Response({'message': 'Profile created Successfully'})
然后,我使用Postman向该REST API发出了请求,例如:
此后,出现以下错误:
Internal Server Error: /services/authentication/signup/
Traceback (most recent call last):
File "/home/suraj/Work/Speeve/speeve-backend/speeveEnv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/suraj/Work/Speeve/speeve-backend/speeveEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/suraj/Work/Speeve/speeve-backend/speeveEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/suraj/Work/Speeve/speeve-backend/speeveEnv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/suraj/Work/Speeve/speeve-backend/speeveEnv/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/suraj/Work/Speeve/speeve-backend/speeveEnv/lib/python3.6/site-packages/rest_framework/views.py", line 488, in dispatch
request = self.initialize_request(request, *args, **kwargs)
File "/home/suraj/Work/Speeve/speeve-backend/speeveEnv/lib/python3.6/site-packages/rest_framework/views.py", line 389, in initialize_request
parsers=self.get_parsers(),
File "/home/suraj/Work/Speeve/speeve-backend/speeveEnv/lib/python3.6/site-packages/rest_framework/views.py", line 266, in get_parsers
return [parser() for parser in self.parser_classes]
File "/home/suraj/Work/Speeve/speeve-backend/speeveEnv/lib/python3.6/site-packages/rest_framework/views.py", line 266, in <listcomp>
return [parser() for parser in self.parser_classes]
TypeError: 'str' object is not callable
[06/Apr/2020 06:45:47] "POST /services/authentication/signup/ HTTP/1.1" 500 96805
我已在设置中将rest_framework
添加到INSTALLED_APPS
列表中。而且我还将REST_FRAMEWORK
添加到了我的设置中,如下:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
],
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FileUploadParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser',
],
}
要获得用于注册用户才能使用的REST API,我必须进行哪些必要的更改?
请帮助
干杯!
答案 0 :(得分:0)
请确保在Postman的标题选项卡中将Content-Type标题设置为用于处理表单数据的正确值。
Content-Type : application/x-www-form-urlencoded
此外,您还可以尝试通过选择raw,然后在最右侧的选项卡中选择json,以json格式发送表单数据。还将Content-Type标头设置为
Content-Type : application/json