通过编写内部方法并正确路由它们,可以使用单个Flask应用程序实现多个网页吗?
from flask import Flask,request,redirect
from flask import render_template
app = Flask(__name__)
@app.route("/")
def admin_login():
return render_template('admin_login.html')
@app.route('/login', methods = ['POST'])
def login():
un = request.form['un']
pwd = request.form['pwd']
print un,pwd
@app.route("/login")
def adminPage1():
return render_template('adminPage1.html')
@app.route("/login/addnew")
def addnew():
return render_template('adminPage2.html')
@app.route("/login/updatestatus")
def updatestatus():
return render_template('adminPage3.html')
admin_login.html
:
<form action="{{url_for('login')}}" method="post">
<fieldset>
<div align="left">
<p><a href="home.html"><strong>Return to home page</strong></a></p>
</div>
<p><strong>Username:</strong><input type="text" name="un" ></p>
<p><strong>Password:</strong><input type="password" name="pwd"></p>
<h4>If you have forgot your password,kindly enter your username and click on forgot password</h4>
<p><a href="forgotpassword.html">forgot Password?</a></p>
<p><input type="reset"><input type="submit" name="submit" value="Login"></p>
</fieldset>
</form>
adminPage1.html
:
<form action="{{url_for('addnew')}}" method="post">
<p><input type="submit" value="Add New" onclick="location.href='adminPage2.html'"></p>
</form>
<form action="{{url_for('updatestatus')}}" method="post">
<p><input type="submit" value="Update Status" onclick="location.href='adminPage3.html'"></p>
</form>
答案 0 :(得分:2)
您尝试实施的是路线的前缀。尝试使用蓝图:
在您的视图(login.py)中定义蓝图:
blueprint = Blueprint('login', __name__, url_prefix='/login')
在视图上方,您可以像在应用程序上一样定义此蓝图的路径:
@blueprint.route('/', methods=['GET', 'POST'])
def admin_login():
# Located at https://yourdomain.com/login
return render_template('admin_login.html')
@blueprint.route('/login/', methods=['GET', 'POST'])
def login():
# Located at https://yourdomain.com/login/login
return render_template('adminPage1.html')
@blueprint.route('/addnew/', methods=['GET', 'POST'])
def adminPage1():
# Located at https://yourdomain.com/login/addnew
return render_template('adminPage1.html')
然后,您可以在__init__.py
:
from application.login import blueprint
application =- Flask(__name__)
application.register_blueprint(blueprint)
有关蓝图的更多信息,请参阅Blueprints
上的Flask文档