'Display an error message within the Login page when the password is wrong
I'm making a form within a website to register and then log in. Both registration and login work without problems, but within the job I must do is that, when I enter the password incorrectly in the Login, it sends me an error message that the password is incorrect. I've found some ways to do it, but when I try it, the page goes blank with the text "none" Here's the code:
import sqlite3 as sql
import web
web.config.debug = False
urls = (
'/login', 'Login',
'/signup', 'Signup',
'/inicio', 'Inicio',
)
app = web.application(urls, globals())
render = web.template.render('views')
class Login:
def GET(self):
return render.login()
def POST(self):
import sys
try:
formulario = web.input()
email = formulario.email
password = formulario.password
conn = sql.connect('test.db')
cur = conn.cursor()
statement = f"SELECT * from USUARIOS WHERE EMAIL='{email}' AND PASSWORD='{password}'" #crea una variable donde se almacenara la consulta a la base de datos con los datos que ingresamos en el formulario
cur.execute(statement) #ejecuta la consulta
if cur.fetchone():#se comprueba el retorno de datos
return web.seeother("inicio")#redirecciona a otra pagina web
except Exception:
print(sys.stderr, "Login Failed")
return render.login("usuario incorrecto")
class Signup:
def GET(self):
return render.signup()
def POST(self):
try:
formulario = web.input()
email = formulario.email
password = formulario.password
print("el correo es ", email, " y la contraseña es ", password)
conn = sql.connect('test.db')
cur = conn.cursor()
insertStat = f"INSERT INTO USUARIOS(EMAIL, PASSWORD) VALUES ('{email}', '{password}')"#crea una variable donde se almacenara la operacion de insertar a la base de datos con los datos que ingresamos en el formulario
cur.execute(insertStat)
conn.commit()#guardamos los dato que hemos ingresado
statement = f"SELECT * from USUARIOS WHERE EMAIL='{email}' AND PASSWORD='{password}'"#crea una variable donde se almacenara la consulta a la base de datos con los datos que ingresamos en el formulario
cur.execute(statement) #ejecuta la consulta
if cur.fetchone():#se comprueba el retorno de datos
return web.seeother("login")#redirecciona a otra pagina web
except Exception:
class Inicio:
def GET(self):
return render.inicio() # renderiza a inicio.html
if __name__ == "__main__":
app.run()
It is supposed that in the Except should place for error, but the forms that I tried or did not usually work or send me to the same screen with the NONE.
Html:
$def with (message)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<p>$message</p>
<h1>Login</h1>
<form action="" method="POST">
<label for="">CORREO</label>
<input type="email" name="email" id="email">
<label for="">CONTRASEÑA</label>
<input type="password" name="password" id="password">
<input type="submit" value="Login">
</form>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
