- 新建Flask项目。
- 设置调试模式。
- 理解Flask项目主程序。
- 使用装饰器,设置路径与函数之间的关系。
- 使用Flask中render_template,用不同的路径,返回首页、登录员、注册页。
- 用视图函数反转得到URL,{{url_for(‘login’)}},完成导航条里的链接。
Python代码:
from flask import Flask,render_template app = Flask(__name__) @app.route('/') def base(): return render_template('base.html') @app.route('/sign_in/') def sign_in(): return render_template('sign_in.html') @app.route('/sign_up/') def sign_up(): return render_template('sign_up.html') if __name__ == '__main__': app.run(debug=True)
导航代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../static/js/aiba.js"></script> </head> <body id="myBody"> <nav> <img id="on_off" onclick="mySwitch()" src="http://p2.so.qhimgs1.com/bdr/_240_/t01a98e72069a45fbee.jpg" width="50px"> <a href="">首页</a> <input type="text" placeholder="搜索"> <input type="submit" value="搜索"class="sign-in-button" data-disable-with="搜索"> <a href="{{ url_for('sign_in') }}">登录</a> <a href="{{ url_for('sign_up') }}">注册</a> </nav> </body> </html>