def post(request): user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): username = user_login_form.cleaned_data['username'] password = user_login_form.cleaned_data['password'] user = authenticate(username=username, password=password) if user: """" 仅允许激活用户允许进行登录 """ if user.is_active: login(request, user) # 登录消息 msg = UserMessage() msg.msg_user = user.id msg.msg_content = '欢迎登录' msg.save() url = request.COOKIES.get('url', '/') ret = redirect(url) ret.delete_cookie('url') return ret else: return render(request, 'users/login.html', { 'msg': '该账户未激活,请先去邮箱激活。', }) else: return render(request, 'users/login.html', { 'msg': '用户名或密码错误', }) else: return render(request, 'users/login.html', { 'user_login_form': user_login_form, })
def post(request): user_register_form = UserRegisterForm(request.POST) if user_register_form.is_valid(): email = user_register_form.cleaned_data['email'] password = user_register_form.cleaned_data['password'] # 查找是否已经注册 user_list = UserProfile.objects.filter( Q(username=email) | Q(email=email)) if user_list: return render(request, 'users/register.html', { 'msg': '用户已经存在', }) else: """ 默认未激活,需要点击邮箱链接进行激活 """ user = UserProfile(username=email, email=email) user.is_active = False user.set_password(password) user.save() """ 给用户发送激活邮件并发送欢迎注册消息 """ msg = UserMessage() msg.msg_content = '欢迎注册[在线教育网],请遵守国家相关法律法规' msg.msg_user = user.id msg.save() send_mail_code(email, 1) return HttpResponse('请尽快前往您的邮箱激活,否则无法登录') else: return render(request, 'users/register.html', { 'user_register_form': user_register_form, })
def user_login(request): if request.method == 'GET': return render(request, 'users/login.html') else: user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): email = user_login_form.cleaned_data['email'] password = user_login_form.cleaned_data['password'] user = authenticate(username=email, password=password) if user: if user.is_start: login(request, user) a = UserMessage() a.msg_user = request.user.id a.msg_content = '欢迎登陆谷粒教育网' a.save() url = request.COOKIES.get('url', '/') return redirect(url) else: return HttpResponse('你的邮箱未激活,请去邮箱激活后再登录!') else: return render(request, 'users/login.html', {'msg': '邮箱或者密码错误'}) else: return render(request, 'users/login.html', {'user_login_form': user_login_form})