开始Flask项目

  1. 新建Flask项目。
  2. 设置调试模式。
  3. 理解Flask项目主程序。
  4. 使用装饰器,设置路径与函数之间的关系。
  5. 使用Flask中render_template,用不同的路径,返回首页、登录员、注册页。
  6. 用视图函数反转得到URL,url_for(‘login’),完成导航里的链接。
    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    
    @app.route('/')
    def jianshu():
        return render_template('jianshu.html')
    
    
    @app.route('/login/')
    def login():
        return render_template("login.html")
    
    
    @app.route("/enroll/")
    def enroll():
        return render_template('enroll.html')
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>
            {% block title %}{% endblock %}
            首页</title>
        <link type="text/css" rel="stylesheet" href="{{ url_for('static',filename='css/jianshu.css') }}">
        <script src="{{ url_for('static',filename='js/nightmode.js') }}">
        </script>
        {% block head %}{% endblock %}
    </head>
    <body id="mybody">
    
    
    <nav>
        <img src="https://p1.ssl.qhmsg.com/dr/270_500_/t01745b3fd4078d5a9e.jpg?size=512x512" height="100" width="10">
        <a href="http://www.jianshu.com/"></a>
        <input type="text" name="search">
        <button type="submit">搜索</button>
        <a href="{{ url_for('jianshu') }}">首页</a>
        <a href="{{ url_for('login') }}">登陆</a>
        <a href="{{ url_for('enroll') }}">注册</a>
        <img src="{{ url_for('static',filename='img/dog.png') }}" alt="" height="100">
        <img id="myonoff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" height="100">
    </nav>
    
    
    
    {% block main %}{% endblock %}
    
    </body>
    
    </html>
原文地址:https://www.cnblogs.com/lintingting/p/7779262.html