'Issue about python flask login page
My codes are running and insert the user information in the database(MySQL) but the login cannot work. These codes are bellow.
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<link href="https://fonts.googleapis.com/css2?family=Jost:wght@500&display=swap" rel="stylesheet">
</head>
<body>
<div class="main">
<input type="checkbox" id="chk" aria-hidden="true">
<div class="signup">
<form method="post">
<label for="chk" aria-hidden="false">Sign up</label>
<input type="text" name="txt" placeholder="User name" required="">
<input type="text" name="email" placeholder="Email" required="">
<input type="password" name="pswd" placeholder="Password" required="">
<button id="button-1" onclikc="main_page()" href="/ ">Sign up</button>
</form>
</div>
<div class="login">
<form method="get">
<label for="chk" aria-hidden="true">Login</label>
<input type="email" name="email_1" placeholder="Email" required="">
<input type="password" name="pswd_1" placeholder="Password" required="">
<button id="button-2" onclick="login()">Login</button>
</form>
</div>
</div>
</body>
</html>
MYSQL CODES
CREATE TABLE user_table (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(55) NOT NULL,
email VARCHAR(55) NOT NULL,
userpass VARCHAR(122) NOT NULL);
from flask
import Flask, render_template, request, redirect, session, flash, url_for, escape
from functools
import wraps
from flask_mysqldb
import MySQL
import MySQLdb.cursors
from sympy
import re
app = Flask(__name__, template_folder = "template")
app.config['MYSQL_HOST'] = ''
app.config['MYSQL_USER'] = 'user'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'db'
mysql = MySQL(app)
def register():
if request.method == "POST":
details = request.form
username = details['txt']
password = details['pswd']
email = details['email']
cur = mysql.connection.cursor()
cur.execute("INSERT INTO user_info(username, email, userpass) VALUES (%s, %s, %s)", (username, email, password))
mysql.connection.commit()
cur.close()
return render_template("index.html")
@app.route('/', methods = ["GET", "POST"])
def mainpage():
return register()
@app.route("/", methods = ["POST"])
@app.route("/home", methods = ["GET"])
def login():
if request.method == "POST":
email = request.form["email_1"]
pwd = request.form["pswd_1"]
cur = mysql.connection.cursor()
cur.execute("select * from user_info where email=%s and userpass=%s", (email, pwd))
data = cur.fetchone()
if data:
session['logged_in'] = True
session['username'] = data["username"]
else :
msg = "invalid login"
flash("yanlış bilgi!!", "error_message")
return render_template('home.html', status = True)
def is_logged_in(f):
@wraps(f)
def wrap( * argws, ** kwargs):
if 'logged_in' in session:
return f( * argws, ** kwargs)
else :
return redirect(url_for('login'))
@app.route("/logout")
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('login'))
if __name__ == '__main__':
app.secret_key = "secret_key"
app.run(debug = True)
Can you help me to find the error or How can i connect the another screen in after clicking to the login button. Additionally I cant fix the problems about that data duplication and any message that visualization on the html screen.
Thank you :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
