模板的继承
部门列表
添加部门
编辑部门
在django中支持模板的继承
定义模板:
定义模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>标题</h1>
<div>
{% block content %}{% endblock %}
</div>
<h1>底部</h1>
</body>
</html>
继承模板:
{% extends 'layout.html' %}
{% block content %}
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">修改部门</h3>
</div>
<div class="panel-body">
<form method="post">
{% csrf_token %}
<div class="form-group">
<label>标题</label>
<input type="text" class="form-control" placeholder="标题" name="title"
value="{{ row_object.title }}">
</div>
<button type="submit" class="btn btn-primary">提交</button>
</form>
</div>
</div>
</div>
{% endblock %}
8.用户管理
mysql> desc app01_userinfo;
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(16) | NO | | NULL | |
| password | varchar(64) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
| account | decimal(10,2) | NO | | NULL | |
| create_time | datetime(6) | NO | | NULL | |
| gender | smallint(6) | NO | | NULL | |
| depart_id | bigint(20) | NO | MUL | NULL | |
+-------------+---------------+------+-----+---------+----------------+
insert into app01_userinfo(name,password,age,account,create_time,gender,depart_id)
values("赵本山","666",234,100.99,"2007-12-14",1,9);
insert into app01_userinfo(name,password,age,account,create_time,gender,depart_id)
values("赵武","666",234,100.99,"2007-2-14",2,9);
insert into app01_userinfo(name,password,age,account,create_time,gender,depart_id)
values("王麻子","112233",234,100.99,"2007-1-14",1,9);
目前的数据
mysql> select * from app01_userinfo;
+----+--------+----------+-----+---------+----------------------------+--------+-----------+
| id | name | password | age | account | create_time | gender | depart_id |
+----+--------+----------+-----+---------+----------------------------+--------+-----------+
| 1 | 赵本山 | 666 | 234 | 100.99 | 2007-12-14 00:00:00.000000 | 1 | 9 |
| 2 | 赵武 | 666 | 234 | 100.99 | 2007-02-14 00:00:00.000000 | 2 | 9 |
| 3 | 王麻子 | 112233 | 234 | 100.99 | 2007-01-14 00:00:00.000000 | 1 | 9 |
| 4 | 赵本山 | 666 | 234 | 100.99 | 2007-12-14 00:00:00.000000 | 1 | 9 |
| 5 | 赵武 | 666 | 234 | 100.99 | 2007-02-14 00:00:00.000000 | 2 | 9 |
| 6 | 王麻子 | 112233 | 234 | 100.99 | 2007-01-14 00:00:00.000000 | 1 | 9 |
+----+--------+----------+-----+---------+----------------------------+--------+-----------+
6 rows in set (0.00 sec)
拿到数据库的数据样式:
1 赵本山 100.99 2007-12-14 1 男 9 保安部门
2 赵武 100.99 2007-02-14 2 女 9 保安部门
3 王麻子 100.99 2007-01-14 1 男 9 保安部门
4 赵本山 100.99 2007-12-14 1 男 9 保安部门
5 赵武 100.99 2007-02-14 2 女 9 保安部门
6 王麻子 100.99 2007-01-14 1 男 9 保安部门
新建用户:
原始方法来清理思路(本质):缺点比较麻烦:
1.数据校验:
2.缺少 错误是要有错误天提示
3.页面上每一个字段都需要重新写一遍
4.关联的数据需要手动的去获取
django的组件
form组件(比较菜)+ 代码
modelfrome组件(不菜)
8.1初始from
1.视图views.py
class MYFrom (From):
user = froms.CharField(widget=forms.Input)括号是插件
pwd = from.CharFiled(widget=forms.Input)
email = from.CharFild(widget=forms.Input)
def user_add(request):
if request.method == "GET":
from = MyFrom()
return render(request, 'user_add.html', context)
2.user_add.html
<form method="post">
{{
}}
#<input type="text" class="form-control" placeholder="姓名" name="user">
</form>
<form method="post">
{{from.user}}
{{from.pwd}}
{{from.email}}
#<input type="text" class="form-control" placeholder="姓名" name="user">
</form>