python - Django Rest Framework user authentication -


i have following code.

i using django rest framework.

i want allow users register. send email address, password, username post.

i feel not using django rest framework correctly.

can guys please me code below?

what's best way structure adhere django rest framework principles?

also, form below invalid ... how post error message back?

@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 , secret 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 startup profile',         'here message.',         'from@example.com',         list(email_address))  serializer = userserializer(user) return response(serializer.data) 

start reading django documentation on working forms. can create class form fields , django create html form it, parse post parameters, , error messages.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -