为什么路由“/login”和“/register”不起作用?
Why do the routes "/login" and "/register" not work?
我的Flask应用不是recognizing/usingauth.py
中定义的两条路由,怎么会?
文件结构
错误信息:
Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
路线
http://127.0.0.1:5000/home (WORKS)
http://127.0.0.1:5000/profile (WORKS)
http://127.0.0.1:5000/login (DOES NOT WORK)
http://127.0.0.1:5000/register (DOES NOT WORK)
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/home")
def home():
return render_template("index.html")
@app.route("/profile")
def profile():
return render_template("profile.html")
auth.py
from flask import current_app as app, render_template
@app.route("/login")
def login():
return render_template("login.html")
@app.route("/register")
def register():
return render_template("register.html")
您不能将路由注册到 current_app
,而是必须使用一个名为 Blueprint
的 class,它正是为此目的而构建的(将应用程序拆分为多个文件)。
app.py
from flask import Flask, render_template
from auth import auth_bp
app = Flask(__name__)
# Register the blueprint
app.register_blueprint(auth_bp)
@app.route("/home")
def home():
return render_template("index.html")
@app.route("/profile")
def profile():
return render_template("profile.html")
auth.py
from flask import Blueprint, render_template
# Initialize the blueprint
auth_bp = Blueprint('auth', __name__)
@auth_bp.route("/login")
def login():
return render_template("login.html")
@auth_bp.route("/register")
def register():
return render_template("register.html")
有关详细信息,请参阅 https://flask.palletsprojects.com/en/2.0.x/blueprints/。
您似乎至少有两个文件包含这些路由。在您的 app.py 文件中,您有 /home 和 /profile,它们都可以工作。它们之所以有效,是因为您在那里初始化了 Flask 应用程序。
Flask 提供蓝图来拆分您的应用程序。例如,您可以创建一个名为 auth 的蓝图。
还有一个特定的tutorial on this subject。
我建议将 app 变量的初始化移动到 __init__.py
文件并创建 create_app() 方法 returns app.在此方法中,您也可以注册您的蓝图。
这个方法看起来像:
def create_app():
app = Flask(__name__)
from . import app as application, auth
app.register_blueprint(auth.bp)
app.register_blueprint(application.bp)
return app
例如,您的 auth.py
文件如下所示:
from flask import Blueprint, render_template
bp = Blueprint('auth', __name__)
@bp.route("/login")
def login():
return render_template("login.html")
@bp.route("/register")
def register():
return render_template("register.html")
我的Flask应用不是recognizing/usingauth.py
中定义的两条路由,怎么会?
文件结构
错误信息:
Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
路线
http://127.0.0.1:5000/home (WORKS)
http://127.0.0.1:5000/profile (WORKS)
http://127.0.0.1:5000/login (DOES NOT WORK)
http://127.0.0.1:5000/register (DOES NOT WORK)
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/home")
def home():
return render_template("index.html")
@app.route("/profile")
def profile():
return render_template("profile.html")
auth.py
from flask import current_app as app, render_template
@app.route("/login")
def login():
return render_template("login.html")
@app.route("/register")
def register():
return render_template("register.html")
您不能将路由注册到 current_app
,而是必须使用一个名为 Blueprint
的 class,它正是为此目的而构建的(将应用程序拆分为多个文件)。
app.py
from flask import Flask, render_template
from auth import auth_bp
app = Flask(__name__)
# Register the blueprint
app.register_blueprint(auth_bp)
@app.route("/home")
def home():
return render_template("index.html")
@app.route("/profile")
def profile():
return render_template("profile.html")
auth.py
from flask import Blueprint, render_template
# Initialize the blueprint
auth_bp = Blueprint('auth', __name__)
@auth_bp.route("/login")
def login():
return render_template("login.html")
@auth_bp.route("/register")
def register():
return render_template("register.html")
有关详细信息,请参阅 https://flask.palletsprojects.com/en/2.0.x/blueprints/。
您似乎至少有两个文件包含这些路由。在您的 app.py 文件中,您有 /home 和 /profile,它们都可以工作。它们之所以有效,是因为您在那里初始化了 Flask 应用程序。
Flask 提供蓝图来拆分您的应用程序。例如,您可以创建一个名为 auth 的蓝图。
还有一个特定的tutorial on this subject。
我建议将 app 变量的初始化移动到 __init__.py
文件并创建 create_app() 方法 returns app.在此方法中,您也可以注册您的蓝图。
这个方法看起来像:
def create_app():
app = Flask(__name__)
from . import app as application, auth
app.register_blueprint(auth.bp)
app.register_blueprint(application.bp)
return app
例如,您的 auth.py
文件如下所示:
from flask import Blueprint, render_template
bp = Blueprint('auth', __name__)
@bp.route("/login")
def login():
return render_template("login.html")
@bp.route("/register")
def register():
return render_template("register.html")