22layout : post
33category : web
44title : " Flask快速入门,知识整理"
5- tags : [阅读,网络 ]
5+ tags : [阅读,web ]
66date : 2018-07-10 13:05:25
77---
88&emsp ;&emsp ; Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,
@@ -23,6 +23,7 @@ pip3 install flask
2323
2424
2525#### Flask依赖一个实现wsgi协议的模块:werkzeug
26+ ```
2627from werkzeug.wrappers import Request, Response
2728
2829@Request.application
@@ -32,8 +33,7 @@ def hello(request):
3233if __name__ == '__main__':
3334 from werkzeug.serving import run_simple
3435 run_simple('localhost', 4000, hello)
35-
36-
36+ ```
3737flask依赖wsgi,实现wsgi模块:wsgiref,werkzeug,uwsgi
3838
3939与Django的简单比较
@@ -239,65 +239,60 @@ def zzz(nid):
239239
2402403. @app.route和app.add_url_rule参数
241241@app.route和app.add_url_rule参数:
242- rule, URL规则
243- view_func, 视图函数名称
244- defaults=None, 默认值,当URL中无参数,函数需要参数时,使用defaults={'k':'v'}为函数提供参数
245- endpoint=None, 名称,用于反向生成URL,即: url_for('名称')
246- methods=None, 允许的请求方式,如:["GET","POST"]
247-
248-
249- strict_slashes=None, 对URL最后的 / 符号是否严格要求,
250- 如:
251- @app.route('/index',strict_slashes=False), #当为False时,url上加不加斜杠都行
252- 访问 http://www.xx.com/index/ 或 http://www.xx.com/index均可
253- @app.route('/index',strict_slashes=True) #当为True时,url后面必须不加斜杠
254- 仅访问 http://www.xx.com/index
255- redirect_to=None, 由原地址直接重定向到指定地址,原url有参数时,跳转到的新url也得传参,注意:新url中不用指定参数类型,直接用旧的参数的类型
256- 如:
257- @app.route('/index/<int:nid>', redirect_to='/home/<nid>') # 访问index时,会直接自动跳转到home,执行home的函数,
258- 不执行index的
259-
260- 或
261- def func(adapter, nid):
262- return "/home/888"
263- @app.route('/index/<int:nid>', redirect_to=func)
264-
265- subdomain=None, 子域名访问
266- from flask import Flask, views, url_for
267-
268- app = Flask(import_name=__name__)
269- app.config['SERVER_NAME'] = 'haiyan.com:5000'
270-
271-
272- @app.route("/", subdomain="admin")
273- def static_index():
274- """Flask supports static subdomains
275- This is available at static.your-domain.tld"""
276- return "admin.xxx.com"
277-
278- #动态生成
279- @app.route("/dynamic", subdomain="<username>")
280- def username_index(username):
281- """Dynamic subdomains are also supported
282- Try going to user1.your-domain.tld/dynamic"""
283- return username + ".your-domain.tld"
284-
285-
286- if __name__ == '__main__':
287- app.run()
288- 所有的域名都得与IP做一个域名解析:
289- 如果你想通过域名去访问,有两种解决方式:
290- 方式一:
291- 1、租一个域名 haiyan.lalala
292- 2、租一个公网IP 49.8.5.62
293- 3、域名解析:
294- haiyan.com 49.8.5.62
295- 4、吧代码放在49.8.5.62这个服务器上,程序运行起来
296- 用户可以通过IP进行访问
297- 方式二:如果是自己测试用的就可以用这种方式。先在自己本地的文件中找
298- C:\Windows\System32\drivers\etc 找到HOST,修改配置
299- 然后吧域名修改成自己的本地服务器127.0.0.1
300- 加上配置:app.config["SERVER_NAME"] = "haiyan.com:5000"
242+ rule, URL规则
243+ view_func, 视图函数名称
244+ defaults=None, 默认值,当URL中无参数,函数需要参数时,使用defaults={'k':'v'}为函数提供参数
245+ endpoint=None, 名称,用于反向生成URL,即: url_for('名称')
246+ methods=None, 允许的请求方式,如:["GET","POST"]
247+
248+
249+ strict_slashes=None,对URL最后的 / 符号是否严格要求,如:
250+ @app.route('/index',strict_slashes=False), #当为False时,url上加不加斜杠都行
251+ 访问 http://www.xx.com/index/ 或 http://www.xx.com/index均可
252+ @app.route('/index',strict_slashes=True) #当为True时,url后面必须不加斜杠
253+ 仅访问 http://www.xx.com/index
254+ redirect_to=None, 由原地址直接重定向到指定地址,原url有参数时,跳转到的新url也得传参,注意:新url中不用指定参数类型,直接用旧的参数的类型
255+ 如:
256+ @app.route('/index/<int:nid>', redirect_to='/home/<nid>') # 访问index时,会直接自动跳转到home,执行home的函数,
257+ 不执行index的或
258+ def func(adapter, nid):
259+ return "/home/888"
260+ @app.route('/index/<int:nid>', redirect_to=func)
261+
262+ subdomain=None,子域名访问
263+ from flask import Flask, views, url_for
264+
265+ app = Flask(import_name=__name__)
266+ app.config['SERVER_NAME'] = 'haiyan.com:5000'
267+
268+
269+ @app.route("/", subdomain="admin")
270+ def static_index():
271+ """Flask supports static subdomains
272+ This is available at static.your-domain.tld"""
273+ return "admin.xxx.com"
274+
275+ #动态生成
276+ @app.route("/dynamic", subdomain="<username>")
277+ def username_index(username):
278+ """Dynamic subdomains are also supported
279+ Try going to user1.your-domain.tld/dynamic"""
280+ return username + ".your-domain.tld"
281+
282+
283+ if __name__ == '__main__':
284+ app.run()
285+ 所有的域名都得与IP做一个域名解析:
286+ 如果你想通过域名去访问,有两种解决方式:
287+ 方式一:
288+ 1、租一个域名 haiyan.lalala
289+ 2、租一个公网IP 49.8.5.62
290+ 3、域名解析: haiyan.com 49.8.5.62
291+ 4、吧代码放在49.8.5.62这个服务器上,程序运行起来用户可以通过IP进行访问
292+ 方式二:如果是自己测试用的就可以用这种方式。先在自己本地的文件中找
293+ C:\Windows\System32\drivers\etc 找到HOST,修改配置
294+ 然后吧域名修改成自己的本地服务器127.0.0.1
295+ 加上配置:app.config["SERVER_NAME"] = "haiyan.com:5000"
301296
302297
303298# =============== 子域名访问============
@@ -1011,8 +1006,8 @@ def register():
10111006其他:
10121007蓝图URL前缀:xxx = Blueprint('account', __ name__ ,url_prefix='/xxx')
10131008蓝图子域名:xxx = Blueprint('account', __ name__ ,subdomain='admin')
1014- # 前提需要给配置SERVER_NAME: app.config[ 'SERVER_NAME'] = 'hc.com:5000'
1015- # 访问时:admin.hc.com:5000/login.html
1009+ 前提需要给配置SERVER_NAME: app.config[ 'SERVER_NAME'] = 'hc.com:5000'
1010+ 访问时:admin.hc.com:5000/login.html
10161011
10171012回到顶部(go to top)
10181013##### 十、闪现(flash)
0 commit comments