def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid: user_name = request.POST.get("username", "") if UserProfile.objects.filter(email=user_name): return render(request, 'register.html', { "msg": "用户已存在", 'register_form': register_form }) user_password = request.POST.get("password", "") user_profile = UserProfile() # 一定要加()!!!!!!这个问题我搞了一天才发现!!! user_profile.username = user_name user_profile.email = user_name user_profile.is_active = False user_profile.password = make_password(user_password) user_profile.save() # 发送注册成功消息 user_message = UserMessage() user_message.user = user_profile.id user_message.message_content = '欢迎注册加入神童研究院' user_message.save() send_register_email(user_name, 'register') return render(request, 'login.html')
def post(self, 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_start: login(request, user) # 当登陆成功的时候,给加入一条消息 a = UserMessage() a.message_man = user.id a.message_content = '欢迎登陆' a.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, 'msg': '邮箱或者密码有误' })
def post(self, request): 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.message_man = user.id a.message_content = '欢迎登录' a.save() url = request.COOKIES.get('url', '/') ret = redirect(url) ret.delete_cookie('url') return ret else: return HttpResponse('请去您的邮箱激活,否则无法登陆') else: return redirect(request, 'login.html', { 'msg': '邮箱或密码有误' }) else: return render(request, 'users/login.html', { 'user_login_form': user_login_form })
def post(self, request, path=None): print(request.POST) login_form = UserLoginForm(request.POST) if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] print(f'{username=},{password=}') # user = UserProfile.objects.filter(Q(username=username) | Q(email=username) | Q(phone=username) & Q(password=password)) user = authenticate(username=username, password=password) print(f'{user=}') if user: if user.is_status: login(request, user) # 登录成功后将登录消息保存到UserMessage中 usermessage = UserMessage() usermessage.message_man = request.user.id usermessage.message_content = 'Hello,{}欢迎来到尚硅谷教育网!'.format( request.user.nick_name) usermessage.save() path = request.POST.get('path') # 获取登录跳转前页面的路径 # url = request.COOKIE.get('url', '/') # 从cookie中获取保存的url # ret = redirect('url') # ret.delete_cookie('url') # return ret print(f"{path=}") if path and path != 'None': return redirect(path) # 跳转回之前的页面 return redirect(reverse('index')) return HttpResponse("请激活你的邮箱再登录") return render(request, 'login.html', {'err': '用户名或密码错误'}) return render(request, 'login.html', {'login_form': login_form})
def post(self, 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_start: login(request, user) # 当登陆成功的时候,给加入一条消息 a = UserMessage() a.message_man = user.id a.message_content = '欢迎登陆' a.save() url = request.COOKIES.get('url', '/') ret = redirect(url) ret.delete_cookie( 'url' ) #从COOKIES拿键url的值,只拿一次,用delete_cookie删掉。如:未登录状态下点“开始学习”,跳登录页,登录账号后,跳学习视频页面。之后,未登录状态下点“收藏”,跳登录页,登录账号后,避免仍然跳学习视频页面 return ret else: return HttpResponse('请去您的邮箱激活,否则无法登陆') else: return render(request, 'users/login.html', {'msg': '邮箱或者密码有误'}) else: return render(request, 'users/login.html', {'user_login_form': user_login_form})
def post(self, request): 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'] #这个方法验证的就是username和password,不要换成别的字段 user = authenticate(username=email, password=password) #邮箱和密码验证通过 if user: #已经被激活的用户可以登录 if user.is_start: login(request, user) #用户每次登录成功,就给他发一个系统消息,欢迎他登录 msg = UserMessage() msg.message_man = user.id msg.message_content = '欢迎登录。' msg.save() #取出cookie里存放的url,这是我们来到登录页面前所在的页面,如果能取到值就原路返回,取不到值就去首页 url = request.COOKIES.get('url', '/') #获取我们要重定向到的页面的response对象,但是删除其中的cookie(为了避免以后每次都重定向到这个页面,尤其是ajax请求会受到影响) ret = redirect(url) ret.delete_cookie('url') return ret else: return HttpResponse('请去您的邮箱激活您的账号,否则无法登录') #验证未通过 else: return render(request, 'users/login.html', {'msg': '邮箱或者密码有误'}) #如果邮箱密码填写格式不对 else: return render(request, 'users/login.html', {'user_login_form': user_login_form})
def user_login(request): if request.method == 'GET': return render(request, 'users/login.html') else: # 获取form登陆表单数据 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'] # 验证用户信息,正确返回True user = authenticate(username=email, password=password) # print(user) if user: # 判断用户是否被激活,如果没激活则无法登陆 if user.is_start: # 已激活 login(request, user) # 登陆成功后发一条信息 userMessage = UserMessage() userMessage.message_man = user.id userMessage.message_content = '欢迎登陆...' userMessage.save() # 访问需要登录用户的url,在登陆后接着访问之前的url url = request.COOKIES.get('url', '/') result = redirect(url) result.delete_cookie('url') return result # return redirect(reverse('index')) else: return HttpResponse("你的账号未被激活,请去邮箱激活,否则无法登陆...") else: return render(request, 'users/login.html', {'mgs': '用户名或密码错误'}) else: return render(request, 'users/login.html', {'user_login_form': user_login_form})