龙游住房和城乡建设局网站,站长工具seo优化系统,杭州手机网站制作,互联网站建设 天津前言
Flask是一个轻量级的Web框架#xff0c;用于快速开发Web应用程序。它的设计理念是简洁、灵活和易于扩展#xff0c;非常适合于从简单的单页应用到复杂的大型项目。通过Flask#xff0c;可以创建各种Web应用程序#xff0c;比如博客、电子商务网站、RESTful API等。
…前言
Flask是一个轻量级的Web框架用于快速开发Web应用程序。它的设计理念是简洁、灵活和易于扩展非常适合于从简单的单页应用到复杂的大型项目。通过Flask可以创建各种Web应用程序比如博客、电子商务网站、RESTful API等。
保姆级别 项目体验 Demo 地址 https://github.com/couragesteak/flask_frame_demo
1 安裝
1.1 Flask
Flask3.0.2
Flask-Cors4.0.1测试环境 Python3.10.9
1.2 MySQL 数据库
ubuntu
sudo apt-get install libmysqlclient-devcentos
yum install -y mysql-devel gcc gcc-devel python-develpip install flask-mysqldb -i https://pypi.douban.com/simple否则报错 没有包 MySqldb
2 基础案例 与 蓝图
原创有勇气的牛排
https://www.couragesteak.com/article/457
2.1 蓝图
蓝图Blueprint是一个用于组织和管理应用程序的强大工具。它可以使程序拆分的更小、更模块化从而提高代码的可扩展性。
模块化开发提高代码复用简化路由管理不至于过于庞大团队协作不同人维护不同块
2.2 案例
main.py
from flask import Flask
from flask import render_template
from flask import request
from flask_cors import CORS
import osapp Flask(__name__,template_foldertemplate, # 定义html文件位置static_url_path/, # 定义静态资源路由路径static_folderresource # 定义静态资源文件夹
)# app.config[SECRET_KEY] os.urandom(24) # 生成随机数种子用于产生SessionID
app.secret_key os.urandom(24) # 用于加密会话数据# 允许跨域请求
CORS(app, supports_credentialsTrue)# 导入蓝图
from src.main.controller.index import index
app.register_blueprint(index)# 定义全局拦截器实现自动登录
app.before_request
def before():url request.pathprint(url)# 路由 白名单pass_list [/, /login, /logout, /vcode, /ecode, /register]if url in pass_list or url.endswith(.js)passelse:print(路由不在白名单 需要登录)username request.cookies.get(username)password request.cookies.get(password)print(username, password)if __name__ __main__:app.run(host0.0.0.0, port8081, debugTrue)蓝图 index.py
from flask import Blueprint, render_template
index Blueprint(index, __name__)# http://127.0.0.1:8081
index.route(/)
def home():res dict(name有勇氣的牛排)return render_template(index.html, resultres)3 获取参数 GET、POST
# 参数测试
# http://127.0.0.1:8081/param_test
m_test.route(/param_test, methods[GET, POST])
def param_test():# 获取当前请求的方法类型method request.methoddata dict(nickname有勇氣的牛排,urlhttps://www.couragesteak.com/,)# 根据不同的请求方法获取参数if method GET:# 从GET请求中获取参数id request.args.get(id, None)data[id] idreturn render_template(test_param.html, datadata)elif method POST:# 从POST请求中获取参数id request.form.get(id, None)data[id] idelse:param No valid methodreturn jsonify(data)4 重定向
# 重定向 路由
# http://127.0.0.1:8081/redirect_with_param
m_test.route(/redirect_with_param)
def redirect_with_param():result dict(name有勇氣的牛排)# return redirect(url_for(param_test))return redirect(/)5 模板
这里使用render_template也可以使用其他模板比如 Jinja2
py
# http://127.0.0.1:8081
index.route(/)
def home():res dict(name有勇氣的牛排)return render_template(index.html, resultres)
html模板
divdiva href/首頁/aa href/login登錄/aa href/param_test參數測試/aa href/logout退出登錄/aa href/js/jquery-3.4.1.min.js打開靜態文件/aa href/redirect_with_param重定向測試/a/div首頁 {{ result.name }}
/div6 报错路由 404/500
# 定义404错误页面
app.errorhandler(404)
def not_found(e):return render_template(error_404.html)# 定义500错误页面
app.errorhandler(500)
def internal_error():return render_template(error_500.html)详细文档
https://dormousehole.readthedocs.io/en/latest/quickstart.html