I’m new to programming and I’m following this tutorial: https://www.youtube.com/watch?v=SUC1aTu092w&ab_channel=edureka%21 | I’m trying to make a login system using flask and MySQLdb and I’ve run into this problem, I’m so confused. There’s nothing wrong with the database, I’ve checked multiple times. I’m using VS code. Error: werkzeug.routing.BuildError: Could not build url for endpoint ‘profile’. Did you mean ‘index’ instead?
app.py
JavaScript
x
65
65
1
2
3
4
from flask import Flask, render_template, request, redirect, session, url_for
5
from flask_mysqldb import MySQL
6
import MySQLdb
7
8
app = Flask(__name__)
9
app.secret_key = "123456789"
10
11
app.config["MYSQL_HOST"] = "localhost"
12
app.config["MYSQL_USER"] = "root"
13
app.config["MYSQL_PASSWORD"] = "Root123"
14
app.config["MYSQL_DB"] = "login"
15
16
db = MySQL(app)
17
18
19
@app.route('/', methods=['GET', 'POST'])
20
def index():
21
if request.method == 'POST':
22
if 'username' in request.form and 'password' in request.form:
23
username = request.form['username']
24
password = request.form['password']
25
cursor = db.connection.cursor(MySQLdb.cursors.DictCursor)
26
cursor.execute("SELECT * FROM logininfo WHERE email= %s AND password= %s", (username,password))
27
info = cursor.fetchone()
28
if info is not None:
29
if info['email'] == username and info['password'] == password:
30
session['loginsuccess'] = True
31
return redirect(url_for('/profile'))
32
else:
33
return redirect(url_for('index'))
34
35
return render_template("login.html")
36
37
38
39
@app.route('/new')
40
def new_user():
41
if request.method == "POST":
42
if "one" in request.form and "two" in request.form and "three" in request.form:
43
username = request.form['one']
44
email = request.form['two']
45
password = request.form['three']
46
cur = db.connection.cursor(MySQLdb.cursors.DictCursor)
47
cur.execute("INSERT INTO login.logininfo(name, password, email)VALUES(%s, %s, %s)", (username, password, email))
48
db.connection.commit()
49
return redirect(url_for('index'))
50
return render_template("register.html")
51
52
@app.route('/new/profile')
53
def profile():
54
if session['loginsuccess'] == True:
55
return render_template("profile.html")
56
if __name__ == '__app__':
57
app.run(debug=True)
58
59
60
61
62
63
64
65
login.html:
JavaScript
1
37
37
1
2
3
4
5
6
<!DOCTYPE html>
7
<html lang="en">
8
<head>
9
<meta charset="utf-8" />
10
<title>Hello World!</title>
11
</head>
12
<body>
13
<div>
14
<form action="" method="post">
15
<label>USERNAME</label><br>
16
<input name="username" type="email" placeholder="Username"><br><br>
17
<label>PASSWORD</label><br>
18
<input name="password" type="password" placeholder="Password"><br><br>
19
<input type="submit" value="LOGIN"><br><br>
20
</form>
21
</div>
22
<div>
23
<form action="/new">
24
<label>New Here?</label>
25
<input value="Register" type="submit">
26
</form>
27
</div>
28
29
</body>
30
</html>
31
32
33
34
35
36
37
register.html
JavaScript
1
29
29
1
2
3
4
<!DOCTYPE html>
5
<html lang="en">
6
<head>
7
<meta charset="UTF-8">
8
<title>Register</title>
9
</head>
10
<body>
11
<div>
12
<form method="post">
13
<label>NAME:</label><br>
14
<input type="text" name="one" id="name"><br><br>
15
<label>USERNAME:</label><br>
16
<input type="email" name="two" id="username"><br><br>
17
<label>PASSWORD:</label><br>
18
<input type="password" name="three" id="password"><br><br>
19
<input type="submit" value="CREATE USER">
20
</form>
21
</div>
22
</body>
23
</html>
24
25
26
27
28
29
profile.html
JavaScript
1
26
26
1
2
3
4
5
6
<!DOCTYPE html>
7
<html lang="en">
8
<head>
9
<meta charset="UTF-8">
10
<title>Profile</title>
11
</head>
12
<body>
13
<form action="/">
14
<h1>Login Successful</h1>
15
<label>Welcome to your profile</label>
16
<button type="submit">LOGOUT</button>
17
</form>
18
</body>
19
</html>
20
21
22
23
24
25
26
Advertisement
Answer
Spacing is critical in python. Each line that starts w/ @app.route should start at the far left. Also I fixed your profile route name.
JavaScript
1
58
58
1
from flask import Flask, render_template, request, redirect, session, url_for
2
from flask_mysqldb import MySQL
3
import MySQLdb
4
5
app = Flask(__name__)
6
app.secret_key = "123456789"
7
8
app.config["MYSQL_HOST"] = "localhost"
9
app.config["MYSQL_USER"] = "root"
10
app.config["MYSQL_PASSWORD"] = "Root123"
11
app.config["MYSQL_DB"] = "login"
12
13
db = MySQL(app)
14
15
16
@app.route('/', methods=['GET', 'POST'])
17
def index():
18
if request.method == 'POST':
19
if 'username' in request.form and 'password' in request.form:
20
username = request.form['username']
21
password = request.form['password']
22
cursor = db.connection.cursor(MySQLdb.cursors.DictCursor)
23
cursor.execute("SELECT * FROM logininfo WHERE email= %s AND password= %s", (username,password))
24
info = cursor.fetchone()
25
if info is not None:
26
if info['email'] == username and info['password'] == password:
27
session['loginsuccess'] = True
28
return redirect(url_for('profile'))
29
else:
30
return redirect(url_for('index'))
31
32
return render_template("login.html")
33
34
35
@app.route('/new')
36
def new_user():
37
if request.method == "POST":
38
if "one" in request.form and "two" in request.form and "three" in request.form:
39
username = request.form['one']
40
email = request.form['two']
41
password = request.form['three']
42
cur = db.connection.cursor(MySQLdb.cursors.DictCursor)
43
cur.execute("INSERT INTO login.logininfo(name, password, email)VALUES(%s, %s, %s)", (username, password, email))
44
db.connection.commit()
45
return redirect(url_for('index'))
46
return render_template("register.html")
47
48
49
@app.route('/profile')
50
def profile():
51
if session['loginsuccess'] == True:
52
return render_template("profile.html")
53
54
55
if __name__ == '__app__':
56
app.run(debug=True)
57
58