def post(self, request): # 实例化 login_form = LoginForm(request.POST) if login_form.is_valid(): # 获取用户提交的用户名和密码 user_name = request.POST.get('username', None) pass_word = request.POST.get('password', None) # 成功返回user对象,失败None user = authenticate(username=user_name, password=pass_word) # 如果不是null说明验证成功 if user is not None: if user.is_active: # 只有注册激活才能登录 login(request, user) return HttpResponseRedirect(reverse('index')) else: return render(request, 'login.html', { 'msg': '用户名或密码错误', 'login_form': login_form }) # 只有当用户名或密码不存在时,才返回错误信息到前端 else: return render(request, 'login.html', { 'msg': '用户名或密码错误', 'login_form': login_form }) # form.is_valid()已经判断不合法了,所以这里不需要再返回错误信息到前端了 else: return render(request, 'login.html', {'login_form': login_form})
def login(): form = LoginForm() error = None if request.method == 'GET' and request.args.get('next'): session['next'] = request.args.get('next', None) if form.validate_on_submit(): user = User.query.filter_by( username=form.username.data, ).first() if user: if bcrypt.hashpw(form.password.data, user.password) == user.password: session['username'] = form.username.data if 'next' in session: next = session.get('next') session.pop('next') return redirect(next) else: return redirect(url_for('index')) else: error = "Incorrect password" else: error = "User not found" return render_template('users/login.html', form=form, error=error)
def login(): form = LoginForm() error = None if request.method == 'GET' and request.args.get('next'): session['next'] = request.args.get('next', None) if form.validate_on_submit(): author = User.query.filter_by( username=form.username.data, ).first() if author: if bcrypt.hashpw(form.password.data, author.password) == author.password: session['username'] = form.username.data session['is_admin'] = author.is_admin session['user_id'] = author.id session['organisation_id'] = author.organisation_id if 'next' in session: next = session.get('next') session.pop('next') return redirect(next) else: return redirect(url_for('login_success')) else: error = "Incorrect password" else: error = "User not found" return render_template('users/login.html', form=form, error=error)
def post(self, request, *args, **kwargs): """通过用户名和密码进行登陆""" # 实例化表单对象 login_form = LoginForm(request.POST) # 表单验证 if login_form.is_valid(): username = login_form.cleaned_data["username"] password = login_form.cleaned_data["password"] # if not username: # return render(request, "login.html", {"msg": "请输入用户名"}) # # if not password: # return render(request, "login.html", {"msg": "请输入密码"}) # 通过用户名和密码得到一个user用户对象,验证用户是否存在! user = authenticate(username=username, password=password) if user is not None: # 进行状态保持 login(request, user) return HttpResponseRedirect(reverse("index")) else: return render(request, "login.html", { "msg": "用户名或密码错误", "login_form": login_form }) else: return render(request, "login.html", {"login_form": login_form})
def post(self, request): form = LoginForm(request.POST) if form.is_valid(): username = request.POST.get('userid') password = request.POST.get('pwd') if not all([username, password]): return render(request, 'register.html', {'errmsg': u'请输入用户名或密码'}) user = authenticate(username=username, password=password) # 登录校验 if user is not None: if user.is_active: login(request, user) response = redirect(reverse('wechat:index')) remember = request.POST.get('remember') if remember == 'on': # 记住用户名 response.set_cookie('username', username, max_age=7 * 24 * 3600) else: response.delete_cookie('username') return response else: return render(request, 'login.html', {'errmsg': u'账户未激活'}) else: return render(request, 'login.html', {'errmsg': u'用户名或密码错误'})
def login(): form = LoginForm() error = None if request.method == 'GET' and request.args.get('next'): session["next"] = request.args.get('next', None) if form.validate_on_submit(): users = User.query.filter_by(name=form.name.data).limit(1) if users.count(): user = users[0] if bcrypt.hashpw(form.password.data, user.password) == user.password: session['name'] = form.name.data if 'next' in session: next = session.get('next') session.pop('next') return redirect(next) else: return redirect(url_for('list')) else: error = "Incorrect Username and password" else: error = "Incorrect Username and password" return render_template('users/login.html', form=form, error=error)
def login(): form=LoginForm() if form.validate_on_submit() : user = User.query.filter_by(email=form.email.data).first() if user and bcrypt.check_password_hash(user.password,form.password.data): login_user(user,remember=form.remember_me.data) flash("Login successfully ","success") return redirect(url_for("home")) else : flash("Username or password is incorrect","danger") return render_template("user/login.html",title="Login",form=form)
def post(self, request): form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get("login_username") password = form.cleaned_data.get("login_password") authenticated_user = authenticate(username=username, password=password) if authenticated_user and authenticated_user.is_active: django_login(request, authenticated_user) redirect_to = request.GET.get("next", "home_page") return redirect(redirect_to) else: messages.error(request, "User incorrect or inactive") return render(request, "login_form.html", {"form": form})
def post(self, request): login_form = LoginForm(request.POST) # 获取登录的数据 if login_form.is_valid(): # 验证是否有效 user_name = request.POST.get('username', '') pass_word = request.POST.get('password', '') user = authenticate(username=user_name, password=pass_word) #验证user if user is not None: login(request, user) return HttpResponseRedirect(reversed('index')) else: return render(request, 'login.html', {'msg': '账号密码有误'}) return render(request, 'login.html', {'login_form': login_form}) #账号无效,错误信息回填
def register(req): if req.method == 'POST': uf = LoginForm(req.POST) if uf.is_valid(): username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] number = uf.cleaned_data['num'] User.object.create(username=username, password=password, number=number) User.save() return render(req, 'login.html') else: uf = LoginForm() return render(req, 'register.html')
def login(): form = LoginForm() error = None if request.method == 'GET' and request.args.get('next'): session['next'] = request.args.get('next', None) if form.validate_on_submit(): author = User.query.filter_by( username=form.username.data, ).first() if request.headers.getlist("X-Forwarded-For"): ip = request.headers.getlist("X-Forwarded-For")[0] else: ip = request.remote_addr print('ip', ip) if author: if bcrypt.hashpw(form.password.data, author.password) == author.password: session['username'] = form.username.data log = UserLog(user_id= str(author.id), ipaddr= str(ip), success=True) db.session.add(log) db.session.commit() session['user_id'] = author.id org = Organisation.query.filter_by(id=author.organisation_id).first() if org: session['is_admin'] = author.is_admin session['organisation_id'] = org.id session['organisation'] =org.name if 'next' in session: next = session.get('next') session.pop('next') return redirect(next) else: return redirect(url_for('login_success')) else: log = UserLog(user_id= str(author.id), ipaddr= ip, success=False) db.session.add(log) db.session.commit() flash("Invalid login credentials",'alert-danger') return redirect(url_for('login')) else: flash("Invalid login credentials",'alert-danger') return redirect(url_for('login')) return render_template('users/login.html', form=form, error=error)
def LoginView(request): if request.method == 'GET': register_form = RegisterForm() return render(request, 'user/login.html', {'register_form': register_form}) if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): user = authenticate( username=form.cleaned_data["username"], password=form.cleaned_data["password"], ) if user and user.is_active: login(request, user) messages.success(request, "自动跳转到主页", extra_tags="登录成功!") return HttpResponseRedirect(reverse("sspanel:userinfo"))
def post(self, request): login_form = LoginForm(request.POST) if login_form.is_valid(): user_name = request.POST.get('username', '') pass_word = request.POST.get('password', '') user = authenticate(username=user_name, password=pass_word) if user is not None: if user.is_active: login(request, user) return HttpResponseRedirect(reverse("index.html")) else: return render(request, 'login.html', {"msg": "用户未激活"}) else: return render(request, "login.html", {"msg": "用户名或密码错误"}) else: return render(request, 'login.html', {"login_form": login_form})
def login(req): if req.method == 'POST': uf = LoginForm(req.POST) if uf.is_valid(): username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] user = User.objects.filter(username__exact=username, password__exact=password) if user: req.session['username'] = username return HttpResponseRedirect('/index') else: err = 'incorrect username or pwd,please input again.' return render(req, 'register.html', {'err': err}) else: uf = LoginForm() return render(req, 'login.html', {'uf': uf})
def post(self, request): login_form = LoginForm(request.POST) if login_form.is_valid(): user_name = request.POST.get("username", "") pass_word = request.POST.get("password", "") user = authenticate(username=user_name, password=pass_word) if user is not None: if user.is_active: login(request, user) from django.urls import reverse return HttpResponseRedirect(reverse('index')) #return render(request,'index.html'),之前用render发现一个渲染的问题,一旦登出再登录,页面图片一片空白。 else: return render(request, 'login.html', {'msg': '用户未激活'}) else: return render(request, 'login.html', {'msg': '用户或密码错误'}) else: return render(request, 'login.html', {'login_form': login_form})
def get(self, request): """ Presenta el formulario de login y gestiona el login de un usuario :param request: objeto HttpRequest con los datos de la petición :return: objeto HttpResponse con los datos de la respuesta """ error_message = "" login_form = LoginForm() context = {'errors': error_message, 'login_form': login_form} return render(request, 'users/login.html', context)
def get(self, request): """ Presenta el formulario de login a un usuario :param request: HttpRequest :return: HttpResponse """ context = { 'form': LoginForm() } return render(request, 'login.html', context)
def post(self, request): login_form = LoginForm(request.POST) if login_form.is_valid(): # 获取用户提交的用户名和密码 user_name = request.POST.get('username', None) pass_word = request.POST.get('password', None) # 成功返回user对象,失败None user = authenticate(username=user_name, password=pass_word) # 账号是否激活 # 如果不是null说明验证成功 if user is not None: # 登录 login(request, user) # return render(request, 'index.html') return HttpResponseRedirect(reverse('index')) else: return render(request, 'login.html', {'msg': '用户名或密码错误'}) else: # 表单校验没有通过 return render(request, 'login.html', locals())
def post(self, request): error_message = [] form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('pwd') user = authenticate(username=username, password=password) if user is None: error_message.append('Nombre de usuraio o contraseña incorrecta') else: if user.is_active: django_login(request, user) url = request.GET.get('next', 'home') return redirect(url) else: error_message.append('El usuario no esta activo') context = { 'errors': error_message, 'login_form': form } return render(request, 'users/login.html', context)
def post(self, request): """ Presenta el formulario de login y gestiona el login de un usuario :param request: objeto HttpRequest con los datos de la petición :return: objeto HttpResponse con los datos de la respuesta """ #cambiar el post de la vista error_message = "" login_form = LoginForm(request.POST) if login_form.is_valid(): username = login_form.cleaned_data.get('username') password = login_form.cleaned_data.get('pwd') user = authenticate(username=username, password=password) if user is None: error_message = "Usuario o contraseña incorrecto" else: if user.is_active: django_login(request, user) return redirect(request.GET.get('next', 'photos_home')) else: error_message = "Cuenta de usuario inactiva" context = {'errors': error_message, 'login_form': login_form} return render(request, 'users/login.html', context)
def post(self, request): login_form = LoginForm(request.POST) if login_form.is_valid(): username = request.POST.get('username', '') password = request.POST.get('password', '') # 成功返回user对象,失败None user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) return HttpResponseRedirect(reverse('index')) else: return render(request, 'login.html', { 'msg': '用户未激活', 'login_form': login_form }) else: return render(request, 'login.html', { 'msg': '用户名或密码错误', 'login_form': login_form }) else: return render(request, 'login.html', {'login_form': login_form})
def post(self, request): """ Hace login de un usuario :param request: HttpRequest :return: HttpResponse """ form = LoginForm(request.POST) context = dict() if form.is_valid(): username = form.cleaned_data.get("username") password = form.cleaned_data.get("password") user = authenticate(username=username, password=password) if user is not None: # usuario autenticado request.session["default-language"] = "es" django_login(request, user) url = request.GET.get('next', 'tasks_list') return redirect(url) else: # usuario no autenticado context["error"] = "Wrong username or password" context["form"] = form return render(request, 'login.html', context)
def login_user(request): page = request.GET.get("page", 1) ads = Ad.objects.filter().order_by("-created_at") p = Paginator(ads, settings.ITEMS_PER_PAGE) if request.method == 'GET': form = LoginForm() return render(request, 'login.html', context={ 'form': form, 'page': p.page(page), }) elif request.method == 'POST': form = LoginForm(request.POST) form.is_valid() user = form.get_user(request) if user: login(request, user) return redirect("/") else: form.errors[NON_FIELD_ERRORS] = form.error_class(['Невозможно выполнить вход с этими учетными данными']) return render(request, "login.html", context={ 'form': form, 'page': p.page(page), })
def get(request): form = LoginForm() context = { 'login_form': form } return render(request, 'users/login.html', context)
def get(self, request): context = {"form": LoginForm()} return render(request, "login_form.html", context)