我正在寻找一种在使用烧瓶蓝图时重定向到另一页的方法
from flask import Blueprint, request, render_template, redirect, url_for
import json
user = Blueprint("user",__name__,template_folder='templates')
@user.route("/index")
def index():
return render_template("index.html")
@user.route("/signup")
def signup():
return render_template("signup.html")
@user.route("/login")
def login():
return render_template("login.html")
from models.user_model import User
from app import db
@user.route('/saveuser/', methods=['GET'])
def saveuser():
username = request.args["username"]
emailid = request.args["email"]
password = request.args["password"]
try:
u = User(str(username),str(emailid),password=str(password))
db.session.add(u)
db.session.commit()
except Exception, excp:
return excp
# return redirect('/index')
return render_template("index.html")
在saveuser()中,如果我能够成功插入
,我想重定向到索引页面答案 0 :(得分:2)
使用redirect
和url_for
:
return redirect(url_for('user.index'))
答案 1 :(得分:0)
使用以下工作示例并根据您的需要进行调整:
from flask import Flask, Blueprint, render_template, redirect, url_for
user = Blueprint("user", __name__, template_folder='templates')
@user.route("/index")
def index():
return render_template("index.html")
@user.route('/saveuser/', methods=['GET'])
def saveuser():
# save your user here
return redirect(url_for('user.index'))
app = Flask(__name__)
app.register_blueprint(user)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)