我有以下代码。
我正在使用django rest框架。
我基本上想让用户注册。 我通过POST发送电子邮件地址,密码,用户名。
我觉得我没有正确使用django rest框架。
你能帮我解决下面的代码吗?
构建它以遵守django休息框架原则的最佳方法是什么?
此外,下面的表格是无效的...如何发回错误信息?
@api_view(['POST'])
def user_login(request):
profile = request.POST
if ('id' not in profile or 'email_address' not in profile or 'oauth_secret' not in profile):
return Response(
status=status.HTTP_204_NO_CONTENT)
identifier = profile['id']
email_address = profile['email_address']
oauth_secret = profile['oauth_secret']
firstname = None
if 'first_name' in profile:
firstname = profile['first_name']
lastname = None
if 'last_name' in profile:
lastname = profile['last_name']
bio = None
if 'bio' in profile:
bio = profile['bio']
oauth_token = None
if 'oauth_token' in profile:
oauth_token = profile['oauth_token']
investor = None
if 'investor' in profile:
investor = profile['investor']
user_form = dict()
user_form['username'] = 'l' + identifier
user_form['password1'] = oauth_secret
user_form['password2'] = oauth_secret
user_form['email'] = email_address
photo = None
noConnections = 0
if 'pictureUrl' in profile:
photo = profile['pictureUrl']
if 'numConnections' in profile:
noConnections = profile['numConnections']
try:
user = User.objects.get(username=identifier)
except User.DoesNotExist:
serializer = UserRegisterSerializer(data=user_form)
if serializer.is_valid():
user = serializer.save()
user.first_name = firstname
user.last_name = lastname
user.save()
# Save our permanent token and secret for later.
userprofile = user.get_profile()
userprofile.bio = bio
userprofile.photo = photo
userprofile.no_linked_con = noConnections
userprofile.oauth_token = oauth_token
userprofile.oauth_secret = oauth_secret
userprofile.save()
else:
return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
user = authenticate(username=identifier, password=oauth_secret)
login(request, user)
if not investor:
send_mail(
'Please complete your startup profile',
'Here is the message.',
'from@example.com',
list(email_address))
serializer = UserSerializer(user)
return Response(serializer.data)
答案 0 :(得分:0)
首先阅读Working with forms上的Django文档。您可以为所有表单字段创建一个类,Django将从中创建一个HTML表单,解析POST参数,并帮助处理错误消息。
答案 1 :(得分:-1)
class UserRegistrationSerializer(serializers.Serializer):
email = serilaizers.EmailField()
username = serializers.CharField(max_length=255) # max_length is optional if you give it validate the length of character's set
password = serializers.CharField(max_length=255, write_only=True)
def validate(attrs): # attrs is dictionary such as {'email':'example@m.com', 'username':'tom', 'password': 'test_password'}
# process your required validation here such as checking unique, etc
return attrs
def create(self, validated_data):
password = validated_data.pop('password')
instance = User(**validated_data)
instance.set_password(password)
instance.save() # before saving you can do any data manipulation
return instance
视图中:
serializer = UserRegistrationSerializer(data=request.data)
serializer.is_valid(raise_exception=True/False)
serializer.save()
return Response(serilaizer.data, status.201) # here you should import Response and status from rest-framework path and you can return anything