def backend_login(request): if request.method == 'POST': login_form = LoginForm(request.POST) if login_form.is_valid(): user = authenticate(username=request.POST['username'], password=request.POST['password']) if user is not None: if user.is_active: login(request, user) return HttpResponseRedirect('/') else: error_message = 'Your account is not active' else: error_message = 'Invalid Login' else: login_form = LoginForm() return render_to_response('backend/login.html', locals(), context_instance=RequestContext(request))
def login(request): """后台登录页面""" if request.method == "GET": login_obj = LoginForm() return render(request, "login.html", {"login_obj": login_obj}) elif request.method == "POST": login_obj = LoginForm(request.POST) if login_obj.is_valid(): code = login_obj.cleaned_data.get("check_code") # 获取用户输入验证码 if request.session.get("CheckCode").upper() == code.upper(): # 用户输入验证码和系统生成验证码匹配 username = login_obj.cleaned_data.get("username") # 获取用户输入用户名 request.session["username"] = username # 在session中设置用户名 request.session.clear_expired() # 将所有Session失效日期小于当前日期的数据删除 remember = login_obj.cleaned_data.get("remember") # 用户是否选中一个月内自动登录 if remember: # 用户选中一个月内自动登录 request.session.set_expiry(2592000) # 设置过期时间为一个月之后 return redirect("/backend/index/") # 注册完毕直接跳转登录页面 else: login_obj.add_error("check_code", "验证码错误") # 添加验证码错误信息 return render(request, "login.html", {"login_obj": login_obj})
def login(request): # When already logged in if request.user.is_authenticated: messages.warning( request, '用户 {username}, 你已经登陆'.format(username=request.user.username)) return redirect('index') # Attempt to login login_form = LoginForm(request.POST.dict() or None) if request.method == 'POST' and login_form.is_valid(): username = login_form.cleaned_data.get('username') password = login_form.cleaned_data.get('password') user = auth.authenticate(username=username, password=password) if user: auth.login(request, user) messages.success( request, '欢迎回来, {username}'.format(username=request.user.username)) return redirect('index') else: messages.error(request, '账号或密码错误') return render(request, 'login.html', {'login_form': login_form})