|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
定义对象(关联数据表和读取列表数据)
- # 与siot数据库中的topic主题表对应
- class topic(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String(80), unique=True, nullable=False)
- username = db.Column(db.String(120), nullable=False)
- description = db.Column(db.String(120), nullable=False)
复制代码
定义表单(数据进行查看和编辑使用) - class FormTopic(FlaskForm):
- id = IntegerField('编号')
- name = StringField("主题")
- username = StringField("用户")
- description = StringField("描述")
- submit = SubmitField("提交")
- Cancel = SubmitField("取消")
复制代码
模板文件(数据增删改查图标模板):templates\list_curd.html
- {% extends 'base.html' %}
- {% from 'bootstrap5/table.html' import render_table %}
- {% from 'bootstrap5/pagination.html' import render_pager, render_pagination %}
- {% block content %}
- <h2>{{ title }}</h2>
- {{ render_table(messages,titles, show_actions=True, model=Message,
- view_url=('Tview', [('id', ':id')]),edit_url=('Tedit', [('id', ':id')]),delete_url=('delete', [('id', ':id')]),new_url=url_for('new')) }}
- {{ render_pagination(pagination) }}
- {% endblock %}
复制代码
模板展示:(2中模板的分析,知识展示列表;增加增删改查) 列表:fun_topic 函数 - @app.route("/")
- @app.route("/t") # 浏览器访问使用
- # @login_required
- def fun_topic():
- titles = [
- ("id", "编号"),
- ("name", "主题"),
- ("username", "用户"),
- ("description", "描述"),
- ] # 自定义标题
- page = request.args.get("page", 1, type=int)
- pagination = topic.query.paginate(page=page, per_page=5)
- user = pagination.items
- print(user)
- return render_template(
- "list_curd.html",
- pagination=pagination,
- titles=titles,
- messages=user,
- Message=topic,
- title="37创客siot2.0-topic主题",
- )
复制代码
查看:Tview 函数
- @app.route('/Tview/<id>', methods=['GET', 'POST'])
- def Tview(id):
- aiuser = topic.query.get(id) #topic对象模型,根据id从数据库读取数据
- form = FormTopic()#表单
- form.id.data = aiuser.id
- form.name.data = aiuser.name
- form.username.data = aiuser.username
- form.description.data = aiuser.description
- if form.validate_on_submit():
- return redirect(url_for("fun_topic"))
- if aiuser:
- return render_template('pub_view.html', form=form, message=aiuser)
- return f'没有对应数据 {id} 存在. 返回 <a href="/t">table</a>.'
复制代码
查看模板文件:templates/pub_view.html
- {% extends 'base.html' %}
- {% from 'bootstrap5/form.html' import render_form, render_field, render_form_row %}
- {% block content %}
- <h2>{{title}}</h2>
- <form method="post">
- {{ render_form(form,button_style='success') }}
- </form>
- {% endblock %}
复制代码
编辑数据:templates\pub_edit.html
- {% extends 'base.html' %}
- {% from 'bootstrap5/form.html' import render_form, render_field, render_form_row %}
- {% block title %} edit note {% endblock title %}
- {% block content %}
- <h2>{{title}}</h2>
- <form method='post'>
- {{ render_form(form) }}
- </form>
- {% endblock content %}
复制代码
编辑修改topic:Tedit
- # 编辑数据----------------------------------------------------------------------
- @app.route('/Tedit/<id>', methods=['GET', 'POST'])
- def Tedit(id):
- # 第一步:定义表单和获取数据
- form = FormTopic()
- aiuser = topic.query.get(id)
- # 第三步:点击保存数据
- if form.validate_on_submit():
- # 获取页面数据到数据对象模型
- aiuser.username = form.username.data
- aiuser.name = form.name.data
- aiuser.description = form.description.data
- db.session.commit() # 提交保存数据
- flash("用户信息已经更新!") # 提示数据操作状态
- return redirect(url_for("fun_topic")) # 定向到列表函数页面
- # 第二步:读取数据显示处理
- # 把数据发送到页面表单中
- form.username.data = aiuser.username
- form.name.data = aiuser.name
- form.description.data = aiuser.description
- form.id.data = aiuser.id
- # 提示操作情况
- flash("用户信息读取完成!")
- # 数据显示页面渲染
- return render_template('pub_edit.html', form=form, title='编辑')
复制代码
预设模块:后续完成该功能更,在函数下面编写所需代码即可
新增数据
- # 新增数据----------------------------------------------------------------------
- @app.route('/Tnew', methods=['GET', 'POST'])
- def Tnew():
- return '增加数据预设模块:Tnew'
复制代码 删除数据
- # 删除数据----------------------------------------------------------------------
- @app.route('/Telete/<id>', methods=['GET', 'POST'])
- def Tdelete(id):
- return '删除数据预设模块:delete'+id
复制代码
删除模块:Tdelete
- # 删除数据----------------------------------------------------------------------
- @app.route('/Telete/<id>', methods=['GET', 'POST'])
- def Tdelete(id):
- aiuser = topic.query.get(id)
- flash(f'编号: {id} --{aiuser.username}-----{aiuser.name}-----的用户数据删除完成.')
- if aiuser:
- db.session.delete(aiuser)
- db.session.commit()
- return redirect(url_for("fun_topic"))
- return f'提示: {id} 数据不存在无法删除. 返回 <a href="/t">表格</a>.'
复制代码 增加模块:Tnew
- # 新增数据----------------------------------------------------------------------
- @app.route('/Tnew', methods=['GET', 'POST'])
- def Tnew():
- form = FormTopic()
- aiuser = topic()
- if form.validate_on_submit():
- ms = form.description.data
- uname = form.username.data
- mz = form.name.data
- id = form.id.data
- user = topic(id=id, name=mz, username=uname,description=ms)
- db.session.add(user)
- db.session.commit()
- flash("你的信息已经保存!")
- return redirect(url_for("fun_topic"))
- return render_template('pub_new.html', form=form, title='新增')
复制代码 new模板文件 templates/pub_new.html
- {% extends 'base.html' %}
- {% from 'bootstrap5/form.html' import render_form, render_field, render_form_row %}
- {% block title %} new note {% endblock title %}
- {% block content %}
- <h2>{{title}}</h2>
- <form method='post'>
- {{ render_form(form) }}
- </form>
- {% endblock content %}
复制代码
|
|