def registration(request): print "Registering" # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': print "POST request" user_form = RegistrationForm(request.POST) # If the two forms are valid... if user_form.is_valid(): print "Is valid" # Save the user's form data to the database. user = user_form.save() print "Saved form" # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.is_active = False print "Saving user" user.save() #Create And Send User Activation Email. confirmation_code = ''.join( random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for x in range(33)) print "adding confirmation code to user" p = UserProfile(user=user, confirmation_code=confirmation_code) p.save() title = "Textile Fabric Consultants, Inc. Account Activation" content = "Someone has recently registered at Textilefabric.com. We hope it was you. If so, please follow the link below. If not please disregard this email.\n" + "theftp.dyndns.org:8000/login/activate/" + str( p.confirmation_code) + "/" + user.username send_mail(title, content, '*****@*****.**', [user.email], fail_silently=False) #Update our variable to tell the template registration was successful. registered = True print "sent email" # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. form = RegistrationForm(request.POST) form_html = render_crispy_form(form) result = {'success': registered, 'form_html': form_html} return result
def register(request): if request.session.get('is_login', None): return redirect(reverse('social:index')) if request.method == 'POST': register_form = forms.RegisterForm(request.POST) message = "请检查填写的内容!" if register_form.is_valid(): username = register_form.cleaned_data.get('username') password1 = register_form.cleaned_data.get('password1') password2 = register_form.cleaned_data.get('password2') email = register_form.cleaned_data.get('email') sex = register_form.cleaned_data.get('sex') if password1 != password2: message = '两次输入的密码不同!' return render(request, 'login/register.html', locals()) else: same_name_user = UserProfile.objects.filter(username=username) if same_name_user: message = '用户名已经存在' return render(request, 'login/register.html', locals()) same_email_user = UserProfile.objects.filter(email=email) if same_email_user: message = '该邮箱已经被注册了!' return render(request, 'login/register.html', locals()) new_user = UserProfile() new_user.username = username new_user.password = hash_code(password1) new_user.email = email new_user.sex = sex new_user.save() code = make_confirm_string(new_user) a = send_mail(email, code) print(a) message = '请前往邮箱进行确认!' return render(request, 'login/confirm.html', locals()) else: return render(request, 'login/register.html', locals()) register_form = forms.RegisterForm() return render(request, 'login/register.html', locals())
def post(self, request): register_form = registerForm(request.POST) if register_form.is_valid(): user_name = request.POST.get("email", "") if UserProfile.objects.filter(email=user_name): return render(request, "register.html", { "register_form": register_form, "msg": "用户已经存在!" }) pass_word = request.POST.get("password", "") user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name user_profile.password = make_password(pass_word) user_profile.save() send_register_email(user_name, "register") return render(request, "success.html") else: return render(request, "register.html", {"register_form": register_form})