一.数据写入数据库
1.创建model,模型以类形式存在,属性对应字段,未创建主键,默认创建整数自增主键
2.运行python manage.py makemigrations myApp01 查看需要映射
3.运行python manage.py migrate 执行映射,mysql中会自动创建表

4.index.html中添加一个form表单,请求路由地址save111,请求方式为post请求
5.urls.py中添加路由
6.views中导包,from myApp01 import models
创建save1111,判断如果请求方式是POST,接收code111,name111值(index.html中的form表单的输入内容),将变量存储到模型,执行save方法,存储model对象(映射到数据库),return HttpResponse('存储成功')作用是提示存储成功

7.python manage.py runserver 运行服务器,打开网页,文本框输入,点击存储

8.页面跳转到存储成功,查看数据库数据,更新成功


二.查询数据库所有数据显示
1.创建一个form表单,发送get请求,请求路由地址find222,创建一个submit按钮

2.创建路由

3.views中判断,如果是get请求,从数据库中获取所有的 Component 模型对象并存储在 cplist 变量中,
创建了ctx字典,键为 'cp111',值为获取到的所有 Component 对象的列表 cplist
render
函数来渲染 'show_component.html' 显示,并将 ctx 字典作为参数传递给模板

4.创建show_component.html
创建表格,遍历键'cp111',格式如下

5.网页点击显示数据
6.显示获取的数据

三.精确查询与模糊查询
1.创建form表单,请求方式为post,请求路由地址find222(同查询所有数据一样,不需重新添加路由)
编写一个input输入查询条件,编写两个单选按钮,用于选择精确查询或者模糊查询,编写一个submit按钮

2.views中判断如果请求是post请求,获取输入框数据和单选按钮数据,
如果为精确查询,过滤输入框值与数据库相等的数据列表,查询code为唯一的,所以返回单个列表,用字典ctx接收,并传递给show_one.html渲染显示

- 如果为模糊查询, 使用'__contains'进行模糊查询,返回多个对象,用show_component.html显示


