werkzeug.routing.BuildError:无法为终结点“配置文件”生成url。你是说“索引”吗?

2024-04-26 23:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程新手,我正在学习本教程:https://www.youtube.com/watch?v=SUC1aTu092w&ab_channel=edureka%21|我试图使用flask和MySQLdb创建一个登录系统,我遇到了这个问题,我很困惑。数据库没有问题,我已经检查过多次了。我正在使用VS代码。错误:werkzeug.routing.BuildError:无法为端点“配置文件”生成url。你是说“索引”吗

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>

 





Tags: namebrformappinputreturnifemail
2条回答

在python中,间距是至关重要的。从w/@app.route开始的每一行都应该从最左边开始。我还修复了您的配置文件路径名称


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)

首先,您没有指定错误,这在这里非常重要。至少我能看到你有这个问题:

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

您必须使用:

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

相关问题 更多 >