开始Flask项目
Posted 熊锋阳
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开始Flask项目相关的知识,希望对你有一定的参考价值。
1.新建Flask项目。
2.设置调试模式。
3.理解Flask项目主程序。
4.使用装饰器,设置路径与函数之间的关系。
5.使用Flask中render_template,用不同的路径,返回首页、登录员、注册页。
6.用视图函数反转得到URL,url_for(‘login’),完成导航里的链接。
from flask import Flask from flask import render_template app = Flask(__name__) @app.route(\'/\') def hello_world(): return render_template(\'base.html\') @app.route(\'/login\') def login(): return render_template(\'login.html\') @app.route(\'/regist\') def regist(): return render_template(\'regist.html\') if __name__ == \'__main__\': app.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>base</title> <link rel="stylesheet" href="../static/css/base.css" type="text/css"> <script src="../static/js/base.js"></script> </head> <body id="myBody"> <nav> <img id="myOnOff" onclick="mySwitch()" src="http://pic35.photophoto.cn/20150601/0007020186231436_b.jpg" width="30px"> <a href="">首页</a> <input type="text" ></input> <button type="submit">搜索</button> <a href="">分享</a> <a href="{{ url_for(\'login\')}}">登录</a> <a href="{{ url_for(\'regist\')}}">注册</a> </nav> <div class="area"> <h1 align="center">中国知名旅游城市</h1> </div> <div class="area1"> <div class="img"> <a href="https://baike.baidu.com/item/%E5%B9%BF%E5%B7%9E/72101?fr=aladdin"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1509630548489&di=adba3a1ea0d4feb1feb7eba085647518&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fac6eddc451da81cb483de8c75866d016082431e4.jpg"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E5%B9%BF%E5%B7%9E/72101?fr=aladdin">广东广州</a></div> </div> <div class="img"> <a href="https://baike.baidu.com/item/%E6%9D%AD%E5%B7%9E/147639?fr=aladdin"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1509709677018&di=090fd02acf52e29a496c6c7cff4174df&imgtype=0&src=http%3A%2F%2Fpic108.nipic.com%2Ffile%2F20160831%2F3456584_231548076001_2.jpg"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E6%9D%AD%E5%B7%9E/147639?fr=aladdin">浙江杭州</a></div> </div> <div class="img"> <a href="https://baike.baidu.com/item/%E8%8B%8F%E5%B7%9E/122945?fr=aladdin"> <img src="https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=851415cddaa20cf4529df68d17602053/91ef76c6a7efce1b41805a80a551f3deb58f65d5.jpg"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E8%8B%8F%E5%B7%9E/122945?fr=aladdin">江苏苏州</a></div> </div> <div class="img"> <a href="https://baike.baidu.com/item/%E6%A2%85%E5%B7%9E/2554831"><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1509709808347&di=a7cd917f3a99faea5d8859787a750dd3&imgtype=0&src=http%3A%2F%2Fimg.bimg.126.net%2Fphoto%2FykhiaI9wi-0amBIH-QubnA%3D%3D%2F1456070054525275946.jpg"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E6%A2%85%E5%B7%9E/2554831">广东梅州</a></div> </div> </div> <br> <br> <footer> <div class="footer_box"> design by XFY </div> </footer> </body> </html>
以上是关于开始Flask项目的主要内容,如果未能解决你的问题,请参考以下文章