37创客科创中心

 找回密码
 立即注册
查看: 1099|回复: 1

240609-16 对siot的topic数据的增删改查操作

[复制链接]

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
发表于 2024-6-9 00:56:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
定义对象(关联数据表和读取列表数据)
  1. # 与siot数据库中的topic主题表对应
  2. class topic(db.Model):
  3.     id = db.Column(db.Integer, primary_key=True)
  4.     name = db.Column(db.String(80), unique=True, nullable=False)
  5.     username = db.Column(db.String(120), nullable=False)
  6.     description = db.Column(db.String(120), nullable=False)
复制代码




定义表单(数据进行查看和编辑使用)
  1. class FormTopic(FlaskForm):
  2.     id = IntegerField('编号')
  3.     name = StringField("主题")
  4.     username = StringField("用户")
  5.     description = StringField("描述")
  6.     submit = SubmitField("提交")
  7.     Cancel = SubmitField("取消")
复制代码

模板文件(数据增删改查图标模板):templates\list_curd.html

  1. {% extends 'base.html' %}
  2. {% from 'bootstrap5/table.html' import render_table %}
  3. {% from 'bootstrap5/pagination.html' import render_pager, render_pagination %}

  4. {% block content %}

  5.     <h2>{{ title }}</h2>

  6.     {{ render_table(messages,titles, show_actions=True, model=Message,
  7. view_url=('Tview', [('id', ':id')]),edit_url=('Tedit', [('id', ':id')]),delete_url=('delete', [('id', ':id')]),new_url=url_for('new')) }}
  8.     {{ render_pagination(pagination) }}
  9. {% endblock %}
复制代码







模板展示:(2中模板的分析,知识展示列表;增加增删改查)
列表:fun_topic 函数
  1. @app.route("/")
  2. @app.route("/t")  # 浏览器访问使用
  3. # @login_required
  4. def fun_topic():
  5.     titles = [
  6.         ("id", "编号"),
  7.         ("name", "主题"),
  8.         ("username", "用户"),
  9.         ("description", "描述"),
  10.     ]  # 自定义标题
  11.     page = request.args.get("page", 1, type=int)
  12.     pagination = topic.query.paginate(page=page, per_page=5)
  13.     user = pagination.items
  14.     print(user)
  15.     return render_template(
  16.         "list_curd.html",
  17.         pagination=pagination,
  18.         titles=titles,
  19.         messages=user,
  20.         Message=topic,
  21.         title="37创客siot2.0-topic主题",
  22.     )
复制代码


查看:Tview 函数

  1. @app.route('/Tview/<id>', methods=['GET', 'POST'])
  2. def Tview(id):
  3.     aiuser = topic.query.get(id) #topic对象模型,根据id从数据库读取数据
  4.     form = FormTopic()#表单
  5.     form.id.data = aiuser.id
  6.     form.name.data = aiuser.name
  7.     form.username.data = aiuser.username
  8.     form.description.data = aiuser.description
  9.     if form.validate_on_submit():
  10.         return redirect(url_for("fun_topic"))
  11.     if aiuser:
  12.         return render_template('pub_view.html', form=form, message=aiuser)
  13.     return f'没有对应数据 {id} 存在. 返回 <a href="/t">table</a>.'
复制代码

查看模板文件:templates/pub_view.html
  1. {% extends 'base.html' %}
  2. {% from 'bootstrap5/form.html' import render_form, render_field, render_form_row %}

  3. {% block content %}

  4. <h2>{{title}}</h2>
  5. <form method="post">
  6. {{ render_form(form,button_style='success') }}

  7. </form>
  8. {% endblock %}
复制代码


编辑数据:templates\pub_edit.html
  1. {% extends 'base.html' %}
  2. {% from 'bootstrap5/form.html' import render_form, render_field, render_form_row %}

  3. {% block title %} edit note {% endblock title %}

  4. {% block content %}
  5. <h2>{{title}}</h2>
  6. <form method='post'>
  7.     {{ render_form(form) }}
  8. </form>
  9. {% endblock content %}
复制代码


编辑修改topic:Tedit
  1. # 编辑数据----------------------------------------------------------------------
  2. @app.route('/Tedit/<id>', methods=['GET', 'POST'])
  3. def Tedit(id):
  4.     # 第一步:定义表单和获取数据
  5.     form = FormTopic()
  6.     aiuser = topic.query.get(id)
  7.     # 第三步:点击保存数据
  8.     if form.validate_on_submit():
  9.         # 获取页面数据到数据对象模型
  10.         aiuser.username = form.username.data
  11.         aiuser.name = form.name.data
  12.         aiuser.description = form.description.data
  13.         db.session.commit()  # 提交保存数据
  14.         flash("用户信息已经更新!")  # 提示数据操作状态
  15.         return redirect(url_for("fun_topic"))  # 定向到列表函数页面
  16.     # 第二步:读取数据显示处理
  17.     # 把数据发送到页面表单中
  18.     form.username.data = aiuser.username
  19.     form.name.data = aiuser.name
  20.     form.description.data = aiuser.description
  21.     form.id.data = aiuser.id
  22.     # 提示操作情况
  23.     flash("用户信息读取完成!")
  24.     # 数据显示页面渲染
  25.     return render_template('pub_edit.html', form=form, title='编辑')
复制代码

预设模块:后续完成该功能更,在函数下面编写所需代码即可
新增数据
  1. # 新增数据----------------------------------------------------------------------
  2. @app.route('/Tnew', methods=['GET', 'POST'])
  3. def Tnew():
  4.     return '增加数据预设模块:Tnew'
复制代码
删除数据
  1. # 删除数据----------------------------------------------------------------------
  2. @app.route('/Telete/<id>', methods=['GET', 'POST'])
  3. def Tdelete(id):
  4.     return '删除数据预设模块:delete'+id
复制代码

删除模块:Tdelete
  1. # 删除数据----------------------------------------------------------------------
  2. @app.route('/Telete/<id>', methods=['GET', 'POST'])
  3. def Tdelete(id):
  4.     aiuser = topic.query.get(id)
  5.     flash(f'编号:  {id} --{aiuser.username}-----{aiuser.name}-----的用户数据删除完成.')
  6.     if aiuser:
  7.         db.session.delete(aiuser)
  8.         db.session.commit()
  9.         return redirect(url_for("fun_topic"))
  10.     return f'提示: {id} 数据不存在无法删除. 返回 <a href="/t">表格</a>.'
复制代码
增加模块:Tnew
  1. # 新增数据----------------------------------------------------------------------
  2. @app.route('/Tnew', methods=['GET', 'POST'])
  3. def Tnew():
  4.     form = FormTopic()
  5.     aiuser = topic()
  6.     if form.validate_on_submit():
  7.         ms = form.description.data
  8.         uname = form.username.data
  9.         mz = form.name.data
  10.         id = form.id.data
  11.         user = topic(id=id, name=mz, username=uname,description=ms)
  12.         db.session.add(user)
  13.         db.session.commit()
  14.         flash("你的信息已经保存!")
  15.         return redirect(url_for("fun_topic"))
  16.     return render_template('pub_new.html', form=form, title='新增')
复制代码
new模板文件  templates/pub_new.html
  1. {% extends 'base.html' %}
  2. {% from 'bootstrap5/form.html' import render_form, render_field, render_form_row %}

  3. {% block title %} new note {% endblock title %}

  4. {% block content %}
  5. <h2>{{title}}</h2>
  6. <form method='post'>
  7.     {{ render_form(form) }}
  8. </form>
  9. {% endblock content %}
复制代码














回复

使用道具 举报

45

主题

84

帖子

909

积分

版主

Rank: 7Rank: 7Rank: 7

积分
909
发表于 2024-6-16 19:51:35 | 显示全部楼层
动态模板案例:
  1. @app.route("/") #浏览器访问使用
  2. @app.route("/t") #浏览器访问使用
  3. def fun_topic():
  4.     titles = [
  5.         ("id", "编号"),
  6.         ("name", "主题"),
  7.         ("username", "用户"),
  8.         ("description", "描述"),
  9.     ]  # 自定义标题
  10.     page = request.args.get("page", 1, type=int)
  11.     pagination = topic.query.paginate(page=page, per_page=7)
  12.     user = pagination.items
  13.     print(user)

  14.     return render_template_string('''{% extends 'base.html' %}
  15. {% from 'bootstrap5/table.html' import render_table %}
  16. {% from 'bootstrap5/pagination.html' import render_pager, render_pagination %}
  17. {% block content %}
  18.     <h2>{{ title }}</h2>
  19.     {{ render_table(messages,titles, show_actions=True, model=Message,
  20. view_url=('Tview', [('id', ':id')]),edit_url=('Tedit', [('id', ':id')]),delete_url=('Tdelete', [('id', ':id')]),new_url=url_for('Tnew')) }}
  21.     {{ render_pagination(pagination) }}
  22. {% endblock %}''',pagination=pagination,titles=titles,messages=user,Message=topic,title="37创客siot2.0-topic主题:使用动态html模式",
  23.             )
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|37创客科创中心

GMT+8, 2025-12-11 18:23 , Processed in 0.240993 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表