def signup(request): if request.method == 'POST': # if the form on the sign in page was actually submitted # it was submitted hence collect data username = request.POST['Username'] password = request.POST['Password'] email = request.POST['Email'] first_name = request.POST['first_name'] last_name = request.POST['second_name'] form = signupform(request.POST) if form.is_valid(): try: # check if the user exists u = User.objects.get(username__exact=username) message = 'The specified user already exists.' return render(request, 'accounts/signup/index.html', {'message': message, 'form': form}) except User.DoesNotExist: # if not create new user user = User.objects.create_user(username, email, password) user.first_name = first_name user.last_name = last_name user.save() profile = UserProfile.objects.create(user=user, role='') profile.save() user = authenticate(username=username, password=password) login(request, user) return HttpResponseRedirect('/') else: form = signupform() message = 'The submitted form was invalid, please fill it again.' return render(request, 'accounts/signup/index.html', {'message': message, 'form': form}) else: # probably a normal get request so return the form form = signupform() return render(request, 'accounts/signup/index.html', {'form': form})
def signup(request): if request.method == 'POST': # if the form on the sign in page was actually submitted # it was submitted hence collect data username = request.POST['Username'] password = request.POST['Password'] email = request.POST['Email'] first_name = request.POST['first_name'] second_name = request.POST['second_name'] phone_number = request.POST['phone_number'] form = signupform(request.POST) if form.is_valid(): try: # check if the user exists u = User.objects.get(username__exact=username) message = 'The specified user already exists.' return render(request, 'accounts/signup/index.html', { 'message': message, 'form': form }) except User.DoesNotExist: # if not create new user user = User.objects.create_user(username, email, password) user.first_name = first_name user.last_name = second_name user.is_active = 0 # since we are yet to verify if the user is a real person user.save() profile = UserProfile.objects.create(user=user, phone_number=phone_number) # generate a token for the email token = str(token_generator()) email_verification = EmailVerification.objects.create( user=user, email_token=token, active=1, verified=0 ) # we save email verification request before sending the email send_mail(user.email, token) return HttpResponse( 'An email was sent to: ' + user.email + '. Please follow the link on your email to activate your account.' ) else: form = signupform() message = 'The submitted form was invalid, please fill it again.' return render(request, 'accounts/signup/index.html', { 'message': message, 'form': form }) else: # probably a normal get request so return the form form = signupform() return render(request, 'accounts/signup/index.html', {'form': form})
def signup(request): if request.method == "POST": # if the form on the sign in page was actually submitted # it was submitted hence collect data username = request.POST["Username"] password = request.POST["Password"] email = request.POST["Email"] first_name = request.POST["first_name"] second_name = request.POST["second_name"] phone_number = request.POST["phone_number"] form = signupform(request.POST) if form.is_valid(): try: # check if the user exists u = User.objects.get(username__exact=username) message = "The specified user already exists." return render(request, "accounts/signup/index.html", {"message": message, "form": form}) except User.DoesNotExist: # if not create new user user = User.objects.create_user(username, email, password) user.first_name = first_name user.last_name = second_name user.is_active = 0 # since we are yet to verify if the user is a real person user.save() profile = UserProfile.objects.create(user=user, phone_number=phone_number) # generate a token for the email token = str(token_generator()) email_verification = EmailVerification.objects.create( user=user, email_token=token, active=1, verified=0 ) # we save email verification request before sending the email send_mail(user.email, token) return HttpResponse( "An email was sent to: " + user.email + ". Please follow the link on your email to activate your account." ) else: form = signupform() message = "The submitted form was invalid, please fill it again." return render(request, "accounts/signup/index.html", {"message": message, "form": form}) else: # probably a normal get request so return the form form = signupform() return render(request, "accounts/signup/index.html", {"form": form})