def login(request): """ 用户登录 :param request: :return: """ if request.method == 'GET': return render(request, 'login.html') else: user = request.POST.get('user') pwd = request.POST.get('pwd') # 当前用户对象 user = models.UserInfo.objects.filter(name=user, password=pwd).first() if user: # 只要用户登录成功 # 设置session_key # 权限信息放入session # 菜单信息放入session permission(request.session.session_key) permission.init_permission(user, request) return redirect('/depart/') return render(request, 'login.html')
def login(request): if request.method == "GET": return render(request, 'login.html') user_name = request.POST['user_name'] password = request.POST['password'] user = UserInfo.objects.get(name=user_name, password=password) if user is None: return redirect(request, 'login.html', {'mag': '用户名或密码错误'}) init_permission(user, request) return HttpResponse("...")
def login(request): err_msg = '' if request.method == 'POST': user = request.POST.get('user') pwd = request.POST.get('pwd') en_pwd = hashlib.md5() en_pwd.update(pwd.encode('utf-8')) obj = models.UserProfile.objects.filter(username=user, password=en_pwd.hexdigest()).first() if obj: request.session['user_id'] = obj.pk init_permission(request, obj) return redirect('/index/') err_msg = '账号或密码错误!' return render(request, 'login.html', {'err_msg': err_msg})
def login(request): if request.method == 'POST': user = request.POST.get('user') pwd = request.POST.get('pwd') obj = models.User.objects.filter(name=user, password=pwd).first() if not obj: # 认证失败 return render(request, 'login.html', {"error": '用户名或密码错误'}) # 登录成功后权限信息的初始化 init_permission(request, obj) # 跳转 return redirect(reverse('host_list')) return render(request, 'login.html')
def login(request): if request.method == "POST": uname = request.POST.get("username") pwd = request.POST.get("password") md5 = hashlib.md5() # md5加密 md5.update(pwd.encode("utf-8")) pwd = md5.hexdigest() obj = models.UserProfile.objects.filter(username=uname, password=pwd, is_active=True).first() # 验证密码 if obj: request.session["u_id"] = obj.pk # 添加session init_permission(request, obj) return redirect(reverse('list')) else: return render(request, "login.html", {"arror": "用户或密码输入错误!!!"}) return render(request, "login.html")
def login(request): """ 用户登录 :param request: :return: 用户登录:马帅,UserInfo表中做查询,登录成功后获取两部分数据: 权限 = { "user": {"url":'/app01/user/'}, "user_add": {"url":'/app01/user/add/'}, "user_edit": {"url":'/app01/user/edit/(\d+)'}, "order": {"url":'/app01/order/'}, } 菜单信息 = { 1:{ 'title':'用户管理', 'icon':'fa-clipboard', 'children':[ {'title':'用户列表','url':'/app01/user/'}, ] }, 2:{ 'title':'商品管理', 'icon':'fa-clipboard', 'children':[ {'title':'订单列表','url':'/app01/order/'}, ] } } """ if request.method == 'GET': return render(request, 'app01/login.html', {'msg': '用户名或密码错误'}) # 1. 取到用户名和密码 进行校验 user = request.POST.get('user') pwd = request.POST.get('pwd') user = models.UserInfo.objects.filter(username=user, password=pwd).first() if not user: return render(request, 'app01/login.html', {'msg': '用户名或密码错误'}) permission.init_permission(user, request) return redirect('/app01/user/')
def login(request): """ 用户登录 :param request: :return: """ if request.method == "POST": user = request.POST.get("username") pwd = request.POST.get("password") user_obj = models.AdminInfo.objects.filter(username=user, password=pwd).first() if not user_obj: return render(request, "login.html", {"error": "用户名或密码错误!"}) request.session["user"] = user_obj.username init_permission(user_obj, request) return redirect(reverse("index")) return render(request, "login.html")
def login(request): if request.method == 'POST': user = request.POST.get('username') pwd = request.POST.get('password') md5 = hashlib.md5() md5.update(pwd.encode('utf-8')) pwd = md5.hexdigest() obj = models.UserProfile.objects.filter(username=user, password=pwd, is_active=True).first() if obj: request.session['pk'] = obj.pk init_permission(request, obj) return redirect(reverse('customer_list')) return render(request, 'login.html')
def login(request): error_msg = "" if request.method == "POST": # 取用户名和密码 username = request.POST.get("username") pwd = request.POST.get("password") # 验证 user_obj = UserInfo.objects.filter(username=username, password=pwd).first() if user_obj: # 登录成功 # 调用封装好的初始化函数里面含有权限列表和显示菜单 init_permission(request, user_obj) return redirect("/customer/list/") else: error_msg = "用户名或密码错误" return render(request, "login.html", locals())
def login(request): if request.method == 'POST': user = request.POST.get('user') pwd = request.POST.get('pwd') # 对密码进行加密后校验 md5 = hashlib.md5() md5.update(pwd.encode('utf-8')) obj = models.User.objects.filter(name=user, password=md5.hexdigest()).first() # 认证失败 if not obj: return render(request, 'login.html', {"error": '用户名或密码不正确!'}) # 认证成功 进行权限信息初始化 init_permission(request, obj) return redirect(reverse('index')) return render(request, 'login.html')
def login(request): if request.method == 'POST': user = request.POST.get('username') pwd = request.POST.get('password') md5 = hashlib.md5() md5.update(pwd.encode('utf-8')) pwd = md5.hexdigest() obj = models.UserProfile.objects.filter(username=user, password=pwd, is_active=True).first() if obj: request.session['pk'] = obj.pk # 登录成功 跳转到主页面 init_permission(request, obj) return redirect(reverse('index')) else: # 登录失败 return render(request, 'login.html', {'error': '用户名或密码错误'}) return render(request, 'login.html')
def login(request): if request.method == 'POST': # 1. 获取前端post请求提交的 用户名和密码 user = request.POST.get('username') pwd = request.POST.get('password') md5 = hashlib.md5() md5.update(pwd.encode('utf-8')) pwd = md5.hexdigest() # 2. 去数据库匹配 该用户名和密码 对应的用户对象 是否存在 obj = models.UserProfile.objects.filter(username=user, password=pwd, is_active=True).first() print(obj.name) if obj: # 若用户对象存在 # 3.1 把该用户的 pk 保存到session中 request.session['pk'] = obj.pk # 3.2 初始化权限信息: 通过用户对象查询到该用户的权限信息,重新构建权限信息字典和菜单信息字典并将之保存到session中 init_permission(request, obj) # 3.3 登录成功, 跳转到客户展示页面 return redirect(reverse('customer_list')) return render(request, 'login.html')
def login(request): """ 用户登录 :param request: :return: """ err_msg = {} # today_str = datetime.date.today().strftime("%Y%m%d") # verify_code_img_path = "%s/%s" %(settings.VERIFICATION_CODE_IMGS_DIR,today_str) # if not os.path.isdir(verify_code_img_path):os.makedirs(verify_code_img_path,exist_ok=True) # random_filename = "".join(random.sample(string.ascii_lowercase,4)) # random_code = verify_code.gene_code(verify_code_img_path,random_filename) # cache.set(random_filename, random_code,300) if request.method == 'GET': return render(request, 'login.html') else: user = request.POST.get('user') pwd = request.POST.get('pwd') _verify_code = request.POST.get('verify_code') _verify_code_key = request.POST.get('verify_code_key') user = models.UserInfo.objects.filter(name=user, password=pwd).first() if user: # 只要用户登录成功 # 设置session_key # 权限信息放入session # 菜单信息放入session current_user = models.UserInfo.objects.filter(name=user).first() permission.init_permission(user, request) # return redirect('/index/') return render(request, 'index.html', {'current_user': current_user}) else: err_msg['error'] = "用户名或密码错误!" return render(request, 'login.html', {"error_msg": err_msg})
def login(request): if request.method == "GET": form = LoginFrom() return render(request, 'login.html', {'form': form}) else: form = LoginFrom(request.POST) if form.is_valid(): # print(form.cleaned_data) 通过form验证可以拿到前端的用户名,密码 {'username': '******', 'password': '******'} form.cleaned_data['password'] = md5(form.cleaned_data['password']) user = models.UserInfo.objects.filter(**form.cleaned_data).first() #另外两种 # models.UserInfo.objects.filter(username=form.cleaned_data['username'],password=form.cleaned_data['password']) # models.UserInfo.objects.filter(**{'username':'******','password':123456}) if user: # 将用户信息放置到session中,user是个对象,python只能序列化字典,元组 # 获取当前用户的所有角色 # role_liat = user.roles.all() # print(role_liat) 打印出来的为对象,使用__str__回调函数可以显示中文 permission.init_permission( user, request) # 跟init_permission函数关联,作用权限初始化 return redirect('/index/') else: form.add_error('password', '用户名或密码错误') return render(request, 'login.html', {'form': form})