1. 新建Flask项目。
  2. 设置调试模式。
  3. 理解Flask项目主程序。
  4. 使用装饰器,设置路径与函数之间的关系。
  5. 使用Flask中render_template,用不同的路径,返回首页、登录员、注册页。
    from flask import Flask,render_template
    
    
    app = Flask(__name__)
    
    
    
    @app.route('/')
    def index():
        return render_template('index.html')
    
    @app.route('/login/',methods=['GET','POST'])
    def login():
        return render_template('login.html')
    
    @app.route('/regist/')
    def regist():
        return render_template('regist.html')
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    
    
    index.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>首页</title>
    <link href="../static/css/wow.css" rel="stylesheet" type="text/css">
    
    
    </head>
    <body>
    <a href="http://127.0.0.1:5000/">首页</a>
    <a href="http://127.0.0.1:5000/login/">登录</a>
    <a href="http://127.0.0.1:5000/regist/">注册</a>
    
    <h1>首页</h1>
    </body>
    
    </html>

     

返回登录及注册页面:

 

6.用视图函数反转得到URL,{{url_for(‘login’)}},完成导航条里的链接。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<a href="{{ url_for('index') }}">首页</a>
<a href="{{ url_for('login') }}">登录</a>
<a href="{{ url_for('regist') }}">注册</a>

<h1>首页</h1>
</body>

</html>

 

posted on 2017-11-03 21:11  094吴嘉绿  阅读(164)  评论(0编辑  收藏  举报