0
I’m building a Python login page with Django, Javascript and HTML. What I’m not knowing how to do is show, on the login screen an Alert with the messages "Unauthorized user" and "Incorrect user or password". My question specifically is: how do I send the error message from the views.py file to login.html, capturing the content of that message and showing it inside an Alert. Below my code:
login.html
{% load static %}
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="login">
<div class="app-title">
<h2>Entre no Sistema</h2>
</div>
<form class="login-form" method=POST>
{% csrf_token %}
<div class="control-group">
<input type="text" name=username placeholder="usuário">
</div>
<div class="control-group">
<input type="password" name=password placeholder="senha">
</div>
<input type=submit class="btn" value="Login">
</form>
</div>
</body>
</html>
py views.
from django.http import HttpResponse
from django.shortcuts import render, redirect
import ldap, json, os
def login(request):
usuarios = ler_usuarios_cadastrados()
request.session['username'] = ''
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
if username == 'admin' and password == 'admin':
request.session['username'] = username
request.session['campus'] = 0
request.session['curso'] = 0
return redirect('/geral')
else:
if username not in usuarios:
#aqui deve mostrar a mensagem "Usuário não autorizado"
return redirect('login')
else:
request.session['username'] = username
request.session['campus'] = usuarios[username]['campus_id']
request.session['curso'] = usuarios[username]['curso_id']
username_ldap = 'uid=' + username + ',ou=People,dc=xpto,dc=abacaxi,dc=edu,dc=br'
try:
conn = ldap.initialize('ldap://ldap.xpto.abacaxi.edu.br')
conn.protocol_version = 3
conn.set_option(ldap.OPT_REFERRALS, 0)
conn.simple_bind_s(username_ldap, password)
except ldap.LDAPError:
#aqui deve mostrar a mensagem "Usuário ou senha incorretos"
return redirect('login')
return redirect('/geral')
else:
return render(request,'login.html')
def ler_usuarios_cadastrados():
file_path = os.path.join(os.path.dirname(__file__), 'usuarios_cadastrados.json')
with open(file_path, "r", encoding="utf8") as usuarios_cadastrados:
return json.load(usuarios_cadastrados)
NOTE: All login logic is working correctly. I would just like to include the error messages. Thanks for your help.
thanks thanks for your reply I solved my problem.
– aligenigena