开始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 jianshu2():
        return render_template('jianshu2.html')
    
    
    @app.route('/login/')
    def login():
        return render_template('login.html')
    
    
    @app.route('/register/')
    def register():
        return render_template('register.html')
    
    if __name__ == '__main__':
        app.run(debug=True)

    导航代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <body bgcolor="#5f9ea0"></body>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
    function mySwitch() {
    var oBody=document.getElementById("myBody");
    var oOnoff=document.getElementById("myOnOff");
    if(oOnoff.src.match("bulbon")){
    oOnoff.src="http://www.runoob.com/images/pic_bulboff.gif"
    oBody.style.background="black";
    oBody.style.color="white";
    }else {
    oOnoff.src="http://www.runoob.com/images/pic_bulbon.gif"
    oBody.style.background="white";
    oBody.style.color="black";
    }

    }
    </script>
    </head>
    <body id="myBody">
    <img id="myOnOff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="20px";>
    <body>
    <h1>简书</h1>
    <nav>
    <a href="{{ url_for("jianshu2")}}">首页</a>
    <a href="{{ url_for('login' )}}" class="right">注册</a>
    <a href="{{ url_for('regiter' )}}" class="right">登录</a>
    <a href="">退出</a>
    <input type="text"name="search">
    <button type="submit">搜索</button>
    <a href="">下载</a>
    <a href="">播放记录</a>
    <a href="">最新咨讯</a>
    </nav>
    <br>
    <div class="recommend" >
    <div class="img">
    <a href=""><img src="../image/1.jpg" width="250" height="100" ></a>
    <div class="desc"><a href="../123/image/1.jpg" >简书交友</a></div>
    </div>
    <div class="img">
    <img src="../image/2.jpg" width="250" height="100"></a>
    <div class="desc"><a href="../static/image/2.jpg" >读书</a></div>
    </div>
    <div class="img">
    <img src="../image/3.jpg" width="250" height="100" ></a>
    <div class="desc"><a href="../static/image/2.jpg" >散文</a></div>
    </div>
    <div id="bottom">
    <a href="">联系我们</a>
    <a href="">加入我们</a>
    <a href="">帮助中心</a>
    <a href="">合作伙伴</a>
    </div>

    </div>
    </div>
    </div>
    </body>
    </body>
    </html>

     

     

posted @ 2017-11-03 21:41  103许雅婷  阅读(147)  评论(0)    收藏  举报