def login1(): form = LoginForm() if form.validate_on_submit(): if form.username.data == 'admin' and form.password.data == 'admin': flash("You've been logged in", 'success') return redirect(url_for('home')) else: flash('Wrong username or password', 'danger') return render_template('login1.html', form=form)
def login(): if current_user.is_authenticated: return redirect(url_for('home')) 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.data) next_page = request.args.get('next') return redirect(next_page) if next_page else redirect(url_for('home')) else: flash('Login Unsuccessful. Please check email and password.','danger') return render_template('login.html', form=form)
def login_user(request): context = {} form = LoginForm(label_suffix='') # If the user is logged in already, redirect to their dashboard if request.user.is_authenticated(): return redirect(reverse('dashboard:dash')) # Check if form was submitted if request.method == 'POST': form = LoginForm(request.POST) # If there are no errors in the form if form.is_valid(): email = request.POST['email'] password = request.POST['password'] # Try to grab associated User object with the email and password provided try: user = User.objects.get(email=email) # If a User was returned if user is not None: if user.check_password(password): try: # Use the username from associated User object to authenticate User user_a = authenticate(username=user.username, password=password) # If the User is active if user_a.is_active: # Login and redirect User login(request, user_a) return redirect(reverse('dashboard:dash')) # Return an 'account disabled' message if user is inactive else: messages.add_message(request, messages.INFO, 'Account Disabled') except User.DoesNotExist: context['user_dne'] = "Your username or password was incorrect." else: context['user_dne'] = "Your username or password was incorrect." # Return an 'invalid login' message if no user found for data except User.DoesNotExist: context['user_dne'] = "Your username or password was incorrect." context['form'] = form return render(request, 'login.html', context)
def readrec(request): name = "" roll = "" city = "" if request.method == "POST": # handle POST request formdata = LoginForm(request.POST) if formdata.is_valid(): # data validation roll = (formdata.cleaned_data['roll']) name = (formdata.cleaned_data['name']) city = (formdata.cleaned_data['city']) else: return render(request, "auth.html", {"form": formdata}) else: formdata = LoginForm() # handle other request with an empty form return render(request, "home.html", { "name": name, "roll": roll, "city": city })