【Flask 1】python
1 from flask import Flask 2 #在同级文件夹下新建一个 config.py 3 import config 4 5 app = Flask(__name__) 6 app.config.from_object(config) 7 8 #装饰器 9 @app.route('/') 10 def show(): 11 12 return "http://www.baidu.com" 13 14 if __name__ == "__main__": 15 #启动一个应用服务器 16 app.run()
1 DEBUG = True
==========================================================页面跳转和重定向======
1 from flask import Flask,url_for,redirect 2 3 app = Flask(__name__) 4 5 6 #装饰器 7 @app.route('/') 8 def index(): 9 login_url = url_for('login') 10 return redirect('login_url') 11 return "这是首页" 12 13 @app.route('/login/') 14 def login(): 15 return u'这是登录界面' 16 17 @app.route('/question/<is_login>') 18 def question(is_login): 19 if is_login == '1': 20 return u'这是发布问答页面' 21 else: 22 return redirect(url_for('login')) 23 24 25 if __name__ == "__main__": 26 #启动一个应用服务器 27 app.run(debug=True)
1 from flask import Flask,url_for,redirect 2 3 app = Flask(__name__) 4 5 6 #装饰器 7 @app.route('/') 8 def index(): 9 login_url = url_for('login') 10 return redirect('login_url') 11 return "这是首页" 12 13 @app.route('/login/') 14 def login(): 15 return u'这是登录界面' 16 17 @app.route('/question/<is_login>') 18 def question(is_login): 19 if is_login == '1': 20 return u'这是发布问答页面' 21 else: 22 return redirect(url_for('login')) 23 24 25 if __name__ == "__main__": 26 #启动一个应用服务器 27 app.run(debug=True)
======================================================模板渲染和参数==================================
文件层次结构:
---template01
-------static(存放Css js 图片)
-------templates(存放HTML)
----index.html
-------Flaskm
1 from flask import Flask,render_template 2 3 app = Flask(__name__) 4 #装饰器 5 @app.route('/') 6 def index(): 7 context = { 8 'username':u'正义V领', 9 'id':'110', 10 'img':'http://p3.so.qhimgs1.com/sdr/200_200_/t018f6302eaa3bc5243.jpg', 11 } 12 return render_template("index.html",**context) 13 14 15 if __name__ == "__main__": 16 #启动一个应用服务器 17 app.run(debug=True)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>js生成网页</title> 6 </head> 7 <body> 8 <img src={{img}}> 9 <button>{{username}}</button> 10 <p>{{id}}</p> 11 </body> 12 </html>
=====================================================模板访问属性和字典===================
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>js生成网页</title> 6 </head> 7 <body> 8 <img src={{img}}> 9 <button>{{username}}</button> 10 <p>{{id}}</p> 11 <hr> 12 <p>名字:{{person['name']}}</p> 13 <p>年龄:{{person['age']}}</p> 14 <hr> 15 <button><a href="{{website.baidu}}">百度</a></button> 16 </body> 17 </html>
1 from flask import Flask,render_template 2 3 app = Flask(__name__) 4 #装饰器 5 @app.route('/') 6 def index(): 7 class Person(object): 8 name = '正义V领' 9 age = 22 10 p = Person() 11 context = { 12 'username':u'正义V领', 13 'id':'110', 14 'img':'http://p3.so.qhimgs1.com/sdr/200_200_/t018f6302eaa3bc5243.jpg', 15 'person':p, 16 'website':{ 17 'baidu':'http://www.baidu.com' 18 } 19 } 20 #render_template是渲染模板,**context是传参数字典 21 return render_template("index.html",**context) 22 23 24 if __name__ == "__main__": 25 #启动一个应用服务器 26 app.run(debug=True)
=======================================================if 和for循环====================
=========【if】
1 from flask import Flask,render_template 2 3 app = Flask(__name__) 4 #装饰器 5 @app.route('/<is_login>/') 6 def index(is_login): 7 if is_login == '1': 8 context = { 9 'username':u'正义V领', 10 'age':'110', 11 } 12 return render_template("index.html",user=context) 13 else: 14 return render_template('index.html') 15 16 17 if __name__ == "__main__": 18 #启动一个应用服务器 19 app.run(debug=True)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>js生成网页</title> 6 </head> 7 <body> 8 {% if user and user.age <=18 %} 9 <a href="#">{{user.username}}</a> 10 <a href="#">注销</a> 11 {% else %} 12 <a href="#">登录</a> 13 <a href="#">注册</a> 14 {% endif %} 15 </body> 16 </html>
==========【for】
1.
1 from flask import Flask,render_template 2 3 app = Flask(__name__) 4 #装饰器 5 @app.route('/') 6 def index(): 7 context = { 8 'username':u'正义V领', 9 'age':'110', 10 } 11 return render_template("index.html",user=context) 12 13 14 if __name__ == "__main__": 15 #启动一个应用服务器 16 app.run(debug=True)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>js生成网页</title> 6 </head> 7 <body> 8 {% for k,v in user.items() %} 9 <p>{{k}}:{{v}}</p> 10 {% endfor %} 11 </body> 12 </html>
2.
1 from flask import Flask,render_template 2 3 app = Flask(__name__) 4 #装饰器 5 @app.route('/') 6 def index(): 7 books = [ 8 { 9 'name':'西游记', 10 'author':'吴承恩', 11 'price':120 12 }, 13 { 14 'name':'红楼梦', 15 'author':'曹雪芹', 16 'price':121 17 }, 18 { 19 'name':'水浒传', 20 'author':'施耐庵', 21 'price':123 22 }, 23 { 24 'name':'三国演义', 25 'author':'罗贯中', 26 'price':128 27 } 28 ] 29 return render_template("index.html",books=books) 30 31 32 if __name__ == "__main__": 33 #启动一个应用服务器 34 app.run(debug=True)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>js生成网页</title> 6 </head> 7 <body> 8 <table> 9 <thead> 10 <th>书名</th> 11 <th>作者</th> 12 <th>价格</th> 13 </thead> 14 <tbody> 15 {% for book in books %} 16 <tr> 17 <td>{{book.name}}</td> 18 <td>{{book.author}}</td> 19 <td>{{book.price}}</td> 20 </tr> 21 {% endfor %} 22 </tbody> 23 </table> 24 </body> 25 </html>
======================================================过滤器====================
1 from flask import Flask,render_template 2 3 app = Flask(__name__) 4 #装饰器 5 @app.route('/') 6 def index(): 7 books = [ 8 { 9 'name':u'西游记', 10 'author':u'吴承恩', 11 'price':120 12 }, 13 { 14 'name':u'红楼梦', 15 'author':u'曹雪芹', 16 'price':121 17 }, 18 { 19 'name':u'水浒传', 20 'author':u'施耐庵', 21 'price':123 22 }, 23 { 24 'name':u'三国演义', 25 'author':u'罗贯中', 26 'price':128 27 } 28 ] 29 return render_template("index.html",books=books) 30 31 32 if __name__ == "__main__": 33 #启动一个应用服务器 34 app.run(debug=True)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>js生成网页</title> 6 </head> 7 <body> 8 <p><img src="{{img|default('http://p2.so.qhimgs1.com/t01c6edbc8dc6ede76e.jpg')}}"width="100",height="100"></p> 9 <hr> 10 <p>评论数:({{books|length}})</p> 11 <ul> 12 {% for book in books %} 13 <li> 14 <a>{{book.name}}</a> 15 <p>{{book.author}}</p> 16 </li> 17 {% endfor %} 18 </ul> 19 </body> 20 </html>
=======================================================URL参数====================================
1 from flask import Flask 2 3 app = Flask(__name__) 4 app.config.from_object(config) 5 6 #装饰器 7 @app.route('/article/<id>') 8 def article(id): 9 return u'请求的参数是:%s'%id 10 11 12 if __name__ == "__main__": 13 #启动一个应用服务器 14 app.run() 15 16 17 ''' 18 1.参数放置在<>中 19 2.视图函数需要和参数一致的 20 21 22 '''
1 from flask import Flask,url_for 2 3 app = Flask(__name__) 4 5 6 #装饰器 7 @app.route('/') 8 def index(): 9 print(url_for('my_list')) 10 print(url_for('article',id='abc')) 11 return "Hello World!" 12 13 @app.route('/list/') 14 def my_list(): 15 return 'list' 16 17 @app.route('/article/<id>') 18 def article(id): 19 return u'请求的参数是:%s'%id 20 21 22 if __name__ == "__main__": 23 #启动一个应用服务器 24 app.run(debug=True)
一个二次元的生物

浙公网安备 33010602011771号