一、Flask的路由系统

  1.@app.route()装饰器中的参数

  methods:当前URL地址,允许访问的请求方式

@app.route("/info", methods=["GET", "POST"])
def student_info():
stu_id = int(request.args["id"])
return f"Hello Old boy {stu_id}" #格式化输出

  

  endpoint:反向RUL地址,默认位视图函数名(url_for)

from flask import url_for

@app.route("/info", methods=["GET", "POST"], endpoint="r_info")
def student_info():
print(url_for("r_info")) # /info
stu_id = int(request.args["id"])
return f"Hello Old boy {stu_id}" # 格式化输出

  

  defaults:视图函数的参数默认值

from flask import url_for

@app.route("/info", methods=["GET", "POST"], endpoint="r_info", defaults={"nid": 100})
def student_info(nid):
print(url_for("r_info")) # /info
# stu_id = int(request.args["id"])
print(nid) #
return f"Hello Old boy {nid}" # Python3.6的新特性 f"{变量名}格式化输出"

  

  strict_slashes:url地址结尾符"/" 的控制False:无论结尾“/”是否存在均可以访问,True:结尾必须不是“/”

# 访问地址 : /info
@app.route("/info", strict_slashes=True)
def student_info():
return "Hello Old boy info" # 访问地址 : /infos or /infos/
@app.route("/infos", strict_slashes=False)
def student_infos():
return "Hello Old boy infos"

  redirect_to:url地址重定向

# 访问地址 : /info 浏览器跳转至 /infos
@app.route("/info", strict_slashes=True, redirect_to="/infos")
def student_info():
return "Hello Old boy info" @app.route("/infos", strict_slashes=False)
def student_infos():
return "Hello Old boy infos"

  

  subdomain:子域名前缀subdomian=“wurenxiansheng”这样写可以得到wurenxiansheng.bokeyuan.com前提是app.config["SERVER_NAME"]="bokeyuan.com"

app.config["SERVER_NAME"] = "bokeyuan.com"

@app.route("/info",subdomain="wurenxiansheng")
def student_info():
return "Hello wurenxiansheng info" # 访问地址为: wurenxiansheng.bokeyuan.com/info

  2.动态参数路由:

from flask import url_for

# 访问地址 : http://127.0.0.1:5000/info/1
@app.route("/info/<int:nid>", methods=["GET", "POST"], endpoint="r_info")
def student_info(nid):
print(url_for("r_info",nid=2)) # /info/2
return f"Hello Old boy {nid}" # Python3.6的新特性 f"{变量名}"

  <int:nid>就是在url后定义一个参数接收

  这种动态参数路由,在url_for的时候,一定要将动态参数名+参数值添加进去,否则会抛出参数错误的异常

  

  3.Flask初始化配置

app = Flask(__name__)
app.template_folder = "templates" #指定模板存放路径
app.static_folder = "文件夹名字" #指定静态文件的存放路径
app.static_url_path = "/static" # 指定静态文件访问路径
static_host = None #静态文件的存放服务器

  4.Flask对象配置

  查看默认配置及配置项

app.config == app.default_config

  我们可以把关于Flask的一些配置,单拎出来创建一个独立的类或者文件用来存放用的时候直接导入,但是要记住用app.config.from_object(Obj)引入配置文件

class FlaskSetting(object):
DEBUG = True #打开Flask的debug模式 app.config.from_object(FlaskSetting)

二、Flask中的蓝图

  1.初始Flask蓝图(blueprint)

  把Blueprint理解为不能被run的Flask对象

  创建一个项目然后将目录结构做成如下(这是一个简单的对于数据的增删改查):

  

  static:存放静态文件

  templates:存放html文件

  views:存放视图文件也就是咱们的蓝图(blueprint)

  mysetting.py:关于Flask的一些配置

  我们只看一个查看的功能走一下流程

  views中的select.py文件中的内容:

from flask import Blueprint,render_template

student = [
{"name":"小霞","age":"","gender":"女"},
{"name":"小红","age":"","gender":"女"},
{"name":"小明","age":"","gender":"男"},
{"name":"小亮","age":"","gender":"男"},
{"name":"小兰","age":"","gender":"女"},
] list_student = Blueprint("list_student",__name__)
@list_student.route("/user_list",methods=["GET","POST"])
def user_list():
return render_template("userlist.html",student_list=student)

  userlist.html文件中的内容:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title></title> <!-- Bootstrap -->
</head>
<body>
<a href="">添加学生信息</a>
<table border="1px">
<thead>
<tr>
<th>名字</th>
<th>性别</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for student in student_list %}
<tr>
<td>{{ student.name }}</td>
<td>{{ student.gender }}</td>
<td>{{ student.age }}</td>
<td>
<a href="">删除</a>
<a href="">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

  manager.py文件中的内容:

from app01 import create_app
app = create_app()
if __name__ == '__main__': app.run(debug=True)

  app01中的__init__.py中的内容:

from flask import Flask
from app01.views import select def create_app():
app = Flask(__name__)
app.register_blueprint(select.list_student)
return app

  蓝图内部的视图函数及route不要出现重复

Flask中路由系统以及蓝图的使用的更多相关文章

  1. Flask最强攻略 - 跟DragonFire学Flask - 第七篇 Flask 中路由系统

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...

  2. 第七篇 Flask 中路由系统以及参数

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...

  3. 第七篇 Flask 中路由系统

    1. @app.route() 装饰器中的参数 如果不明白装饰器 点击这里 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", me ...

  4. 7,Flask 中路由系统

    Flask中的路由系统 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道 ...

  5. flask中路由系统

    flask中的路由我们并不陌生,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST"]) 1 ...

  6. Flask中路由系统、Flask的参数及app的配置

    @app.route('/', methods=['GET', 'POST']) 1. @app.route()装饰器中的参数 methods:当前URL地址,允许访问的请求方式 @app.route ...

  7. Flask 中路由系统

    1. @app.route() 装饰器中的参数 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", methods=["G ...

  8. Flask 的路由系统 FBV 与 CBV

    Flask的路由系统 本质: 带参数的装饰器 传递函数后 执行 add_url_rule 方法 将 函数 和 url 封装到一个 Rule对象 将Rule对象 添加到 app.url_map(Map对 ...

  9. Flask中路由参数

    Flask中路由参数.请求方式设置 一.参数设置 1.参数类型 Flask中参数的使用 @app.route('/parames/<username>/') def hello_world ...

随机推荐

  1. php 访问用友u8数据

    <?php namespace app\api\controller; use think\Controller; use think\Db; use think\Log; /** * desc ...

  2. 图形查询属性(IdentifyTask实现查询)//查询本地服务

    主页代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  3. CSS实现图片阴影效果

    <title>无标题文档</title> <style type="text/css"> /*方法一:使用一个GIF文件的方法*/ .gifsh ...

  4. C# HttpClient 请求转发

    最近在做一个项目,需要用到别人的接口,但是遇到一个问题,这个接口只能在服务器上访问,不支持外网访问,这让人有点头疼,本地开发没有对应的环境,不好调试,写好代码封装好发布到服务器,在进行前期测试太麻烦了 ...

  5. swift学习之UIButton

    // //  ViewController.swift //  button // //  Created by su on 15/12/7. //  Copyright © 2015年 tian. ...

  6. Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们。

    Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显示日期的 ...

  7. POJ3281 Dining 2017-02-11 23:02 44人阅读 评论(0) 收藏

    Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and dri ...

  8. 5、Docker架构和底层技术

    5.1 Docker Platform Docker提供了一个开发,打包,运行APP的平台 把APP和底层infrastructure隔离开来 5.2 Docker Engine 后台进程(docke ...

  9. invoke方法

    主要是为了类反射,这样你可以在不知道具体的类的情况下,根据配置的字符串去调用一个类的方法.在灵活编程的时候非常有用.很多框架代码都是这样去实现的.但是一般的编程,你是不需要这样做的,因为类都是你自己写 ...

  10. MSP430 G2553 LaunchPad GPIO中断

    P1.P2端口上的每个管脚都支持外部中断.P1端口的所有管脚都对应同一个中断向量(Interrupt Vector),类似的,P2端口的所有管脚都对应另一个中断向量:通过PxIFG寄存器来判断中断来源 ...