提交 85d4fe7b 编写于 作者: 鞋子不会飞's avatar 鞋子不会飞

蓝图

上级 45ccb42b
......@@ -10,7 +10,7 @@ mkvirtualenv -p python3 wyh
workon wyh
#安装包
pip install -r requirements.txt
pip install -i https://pypi.doubanio.com/simple/ -r requirements.txt
pip install -i https://pypi.doubanio.com/simple/ flask==1.1.2
......
from flask import Blueprint
# 1.创建蓝图对象
# 细节1: 可以使用url_prefix对蓝图注册的所有路由添加统一的资源段前缀
admin_blu = Blueprint("admin", __name__, static_folder='static', static_url_path='/static',
template_folder='templates', url_prefix='/admin')
# 细节3: 蓝图设置请求钩子 只会监听该蓝图注册的路由
@admin_blu.before_request
def news_prepare():
print("news_prepare")
# 只要出现ImportError: cannot import name xx, 一定是导入的内容没有被定义
# 解决办法: 查看并修改代码的执行顺序
# 4.让程序关联视图文件
from . import views # 从当前包中导入views文件
from flask import url_for
from admin import admin_blu
# 2.使用蓝图对象来注册路由
@admin_blu.route('/')
def index():
# 细节2: 蓝图注册的路由, 其视图函数的标记为: 蓝图名.函数名
url1 = url_for('admin.index')
return "admin个人中心-首页"
@admin_blu.route('/basic_info')
def basic_info():
return "admin个人中心-基本信息"
# 导入Flask类
import json
import sys
from flask import Flask
from user import user_blu
from admin import admin_blu
class DefaultConfig(object):
"""默认配置"""
SECRET_KEY = 'TPmi4aLWRbyVq8zu9v82dWYW1'
# Flask类接收一个参数__name__
app = Flask(__name__)
print(sys.path) # 模块查询路径默认包含 解释器目录, 第三方包安装目录, 项目目录
app = Flask(__name__, # 导入名称, 用于查询静态文件/模板文件的存储路径
# 官方建议使用__name__, 如果使用该值, 则会从当前文件所在的目录中查询静态文件/模板文件夹
static_folder="static", # 设置静态文件存储路径
static_url_path="/static_url", # 设置静态文件的访问路径 如 /res/img/123.jpg
template_folder="templates" # 设置模板文件的存储路径
)
# app.config.from_object(DefaultConfig)
app.config.from_pyfile('settings.py')
# 3.注册蓝图对象
app.register_blueprint(user_blu)
app.register_blueprint(admin_blu)
######################################################################应用顶层视图######################################################################
# 装饰器的作用是将路由映射到视图函数index
@app.route('/')
@app.route("/")
def index():
return 'Hello World'
print(app.config['SECRET_KEY'])
return "hello world"
@app.route('/url_map')
def route_map():
"""
主视图,返回所有视图网址
"""
rules_iterator = app.url_map.iter_rules()
return json.dumps({rule.endpoint: rule.rule for rule in rules_iterator})
# 利用methods参数可以自己指定一个接口的请求方式
@app.route("/wyh_test01", methods=["POST"])
def view_func_1():
return "hello world 1"
@app.route("/wyh_test02", methods=["GET", "POST"])
def view_func_2():
return "hello world 2"
@app.route('/<user_id>')
def user_info(user_id):
print(type(user_id))
return 'hello user {}'.format(user_id)
@app.route('/users/test01/<int:user_id>')
def user_info01(user_id):
print(type(user_id))
return 'hello user {}'.format(user_id)
@app.route('/users/test02/<int(min=1):user_id>')
def user_info02(user_id):
print(type(user_id))
return 'hello user {}'.format(user_id)
# Flask应用程序实例的run方法启动WEB服务器
if __name__ == '__main__':
app.run()
print(app.url_map)
app.run(debug=True)
SECRET_KEY = 'TPmi4aLWRbyVq8zu9v82dWYW1'
\ No newline at end of file
from flask import Blueprint
# 1.创建蓝图对象
# 细节1: 可以使用url_prefix对蓝图注册的所有路由添加统一的资源段前缀
user_blu = Blueprint('user_b', __name__, static_folder='static', url_prefix='/user')
# 细节3: 蓝图设置请求钩子 只会监听该蓝图注册的路由
@user_blu.before_request
def news_prepare():
print("news_prepare")
# 只要出现ImportError: cannot import name xx, 一定是导入的内容没有被定义
# 解决办法: 查看并修改代码的执行顺序
# 4.让程序关联视图文件
from . import views # 从当前包中导入views文件
\ No newline at end of file
from flask import url_for
from user import user_blu
# 2.使用蓝图对象来注册路由
@user_blu.route('/')
def index():
# 细节2: 蓝图注册的路由, 其视图函数的标记为: 蓝图名.函数名
url1 = url_for('user_b.index')
return "个人中心-首页"
@user_blu.route('/basic_info')
def basic_info():
return "个人中心-基本信息"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册