0

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





from flask import Flask, render_template, request, redirect, session,  url_for
from flask_mysqldb import MySQL
import MySQLdb

app = Flask(__name__)
app.secret_key = "123456789"

app.config["MYSQL_HOST"] = "localhost"
app.config["MYSQL_USER"] = "root"
app.config["MYSQL_PASSWORD"] = "Root123"
app.config["MYSQL_DB"] = "login"

db = MySQL(app)


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        if 'username' in request.form and 'password' in request.form:
            username = request.form['username']
            password = request.form['password']
            cursor = db.connection.cursor(MySQLdb.cursors.DictCursor)
            cursor.execute("SELECT * FROM logininfo WHERE email= %s AND password= %s", (username,password))
            info = cursor.fetchone()
            if info is not None:
                if info['email'] == username and info['password'] == password:
                    session['loginsuccess'] = True
                    return redirect(url_for('/profile'))
            else:
                return redirect(url_for('index'))

    return render_template("login.html")

    

    @app.route('/new')
    def new_user():
        if request.method == "POST":
            if "one" in request.form and "two" in request.form and "three" in request.form:
                username = request.form['one']
                email = request.form['two']
                password = request.form['three']
                cur = db.connection.cursor(MySQLdb.cursors.DictCursor)
                cur.execute("INSERT INTO login.logininfo(name, password, email)VALUES(%s, %s, %s)", (username, password, email))
                db.connection.commit()
                return redirect(url_for('index'))
        return render_template("register.html")

        @app.route('/new/profile')
        def profile():
            if session['loginsuccess'] == True:
               return render_template("profile.html")
if __name__ == '__app__':
    app.run(debug=True)







login.html:







<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Hello World!</title>
    </head>
    <body>
        <div>
            <form action="" method="post">
                <label>USERNAME</label><br>
                <input name="username" type="email" placeholder="Username"><br><br>
                <label>PASSWORD</label><br>
                <input name="password" type="password" placeholder="Password"><br><br>
                <input type="submit" value="LOGIN"><br><br>
            </form>
        </div>
        <div>
            <form action="/new">
           <label>New Here?</label>
            <input value="Register" type="submit">
        </form>
        </div>
    
    </body>
</html>






register.html





<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Register</title>
    </head>
    <body>
        <div>
            <form method="post">
                <label>NAME:</label><br>
                <input type="text" name="one" id="name"><br><br>
                <label>USERNAME:</label><br>
                <input type="email" name="two" id="username"><br><br>
                <label>PASSWORD:</label><br>
                <input type="password" name="three" id="password"><br><br>
                <input type="submit" value="CREATE USER">
            </form>
        </div>
    </body>
</html>





profile.html







    <!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Profile</title>
    </head>
    <body>
        <form action="/">
            <h1>Login Successful</h1>
            <label>Welcome to your profile</label>
            <button type="submit">LOGOUT</button>
        </form>
    </body>
</html>

 




2 Answers 2

2

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.


from flask import Flask, render_template, request, redirect, session,  url_for
from flask_mysqldb import MySQL
import MySQLdb

app = Flask(__name__)
app.secret_key = "123456789"

app.config["MYSQL_HOST"] = "localhost"
app.config["MYSQL_USER"] = "root"
app.config["MYSQL_PASSWORD"] = "Root123"
app.config["MYSQL_DB"] = "login"

db = MySQL(app)


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        if 'username' in request.form and 'password' in request.form:
            username = request.form['username']
            password = request.form['password']
            cursor = db.connection.cursor(MySQLdb.cursors.DictCursor)
            cursor.execute("SELECT * FROM logininfo WHERE email= %s AND password= %s", (username,password))
            info = cursor.fetchone()
            if info is not None:
                if info['email'] == username and info['password'] == password:
                    session['loginsuccess'] = True
                    return redirect(url_for('profile'))
            else:
                return redirect(url_for('index'))

    return render_template("login.html")

    
@app.route('/new')
def new_user():
    if request.method == "POST":
        if "one" in request.form and "two" in request.form and "three" in request.form:
            username = request.form['one']
            email = request.form['two']
            password = request.form['three']
            cur = db.connection.cursor(MySQLdb.cursors.DictCursor)
            cur.execute("INSERT INTO login.logininfo(name, password, email)VALUES(%s, %s, %s)", (username, password, email))
            db.connection.commit()
            return redirect(url_for('index'))
    return render_template("register.html")


@app.route('/profile')
def profile():
    if session['loginsuccess'] == True:
        return render_template("profile.html")


if __name__ == '__app__':
    app.run(debug=True)

1
  • 1
    WOW. It worked, Thank you so much. I have been spending hours to fix this everyday for like so long and all it was were the spacings. Thanks man!
    – Fat Trat
    Mar 9, 2021 at 5:26
0

At first, you didn't specify the error, which is very important here. At least what I can see is you have this problem:

if __name__ == '__app__':
    app.run(debug=True)

And you have to use:

if __name__ == '__main__':
    app.run(debug=True)
1
  • werkzeug.routing.BuildError: Could not build url for endpoint 'profile'. Did you mean 'index' instead? My guy this is the error ^. Also, that didn't work, but thanks anyway. Could you please check and see what the problem is here?
    – Fat Trat
    Mar 8, 2021 at 15:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.