Flask-----SQLAlchemy教程

存session session'username' = username # 存储数据到 session

取session username = session.get('username')

render_template**return render_template('index.html', username='Alice')** ,渲染一个包含 username 变量的模板。

redirect return redirect(url_for('profile', username='Alice')),重定向到个人主页

添加

①get_json从前端获取数据

②加入类实例

③添加进入db

④提交db

data = request.get_json() # 获取 JSON 格式的数据
username = data.get('username')
email = data.get('email')

new_user = User(username=username, email=email)
db.session.add(new_user)
db.session.commit()

查找

实例类.query.get

=======================================

贴吧和评论

获取某个帖子的所有评论

post = Post.query.get(post_id)

comments = Comment.query.filter_by(post_id=post_id).all()

在帖子下添加评论

new_comment = Comment(content=content, user_id=user_id, post_id=post_id)

======================================

表说明

user
用户id
用户账号
用户密码
用户信息
用户头像

post
帖子id
用户id
帖子内容
帖子图片
帖子时间
(创建帖子)
获取帖子所有评论(在comment表里面找到所有与该帖子id)

comment
评论id
用户id
帖子id
评论内容
评论时间,

product
商品id
商品名字
商品价格
(上架商品)

order
订单id
用户id
订单时间
订单状态
商品id
创建新订单(点就按钮当前用户id,还有商品的id)
获取用户的所有订单(通过用户id先把所有商品id找出来,然后商品id在在推出商品的内容)

相关推荐
行者-全栈开发11 分钟前
SpringBoot + GraalVM 原生编译实战:启动时间从8秒到0.5秒的极致优化
java·spring boot·后端
tryCbest20 分钟前
Python 爬虫从入门到实战(一):基础篇——从零写出第一个爬虫
开发语言·爬虫·python
CoderYanger22 分钟前
Java EE 进阶:10. MyBatis-Plus使用(第一弹)
java·spring boot·后端·sql·spring·java-ee·mybatis
无小道23 分钟前
Python语句的特色
python·语句
一条Go1 小时前
状态页与 SLA:对外承诺是怎么算出来的
后端·架构·github
2601_967980111 小时前
python pytest如何编写测试
python·测试用例·pytest·函数·assert
喵个咪2 小时前
GoWind IM 场景:乙方怎么用一套 IM 平台交付多个客户——档位、租户与离线交付
后端·go·saas
2601_967980113 小时前
Python中heapq模块的各种用法实例
数据结构·python·算法·应用实例·heapq模块
大胖爱学习_MJ3 小时前
超越向量检索:用 Spring Boot + Neo4j 手搓 GraphRAG 知识图谱记忆
java·后端·python
RobinDevNotes3 小时前
24GB Mac 做数字人:声音放本地,画面租云端
人工智能·python·macos·开发工具