创建老师表
use vote;
create table `tb_teacher` (
`no` integer not null auto_increment comment '编号',
`name` varchar(20) not null comment '姓名',
`sex` boolean not null default 1 comment '性别',
`birth` date not null comment '出生日期',
`intro` varchar(1000) not null default '' comment '介绍',
`photo` varchar(255) not null default 'default.png' comment '照片',
`gcount` integer not null default 0 comment '好评数',
`bcount` integer not null default 0 comment '差评数',
`sno` integer not null comment '所属学科',
primary key (`no`),
foreign key (`sno`) references `tb_subject` (`no`)
);
把表映射到models.py中
django虚拟环境终端中
python manage.py inspectdb tb_subject tb_teachers > polls/models.py
#映射指定的表到models.py中,避免吧django中的表映射过去
自定义映射表的类
from django.db import models
class Subject(models.Model):
no = models.AutoField(primary_key=True,verbose_name='编号') #django后台起中文名verbose_name参数
name = models.CharField(max_length=20,verbose_name='名称')
intro = models.CharField(max_length=1000, blank=True, null=True,verbose_name='介绍')
is_hot = models.BooleanField(default=False, null=True,verbose_name='是否热门')
class Meta:
managed = False
db_table = 'tb_subject'
verbose_name = '学科'
verbose_name_plural = '学科'
SEX_OPTIONS = (
(True,'男'),
(False,'女')
)
class Teachers(models.Model):
no = models.AutoField(primary_key=True,verbose_name='编号')
name = models.CharField(max_length=10,verbose_name='姓名')
sex = models.BooleanField(default=True,choices=SEX_OPTIONS,verbose_name='性别')
#为了比较直观,为True时显示男,为False时显示女
birth = models.DateField(verbose_name='出生日期')
intro = models.CharField(max_length=1000, blank=True, null=True,verbose_name='介绍')
good_count = models.IntegerField(blank=True, null=True,verbose_name='好评')
bad_count = models.IntegerField(blank=True, null=True,verbose_name='差评')
photo = models.ImageField(max_length=100,verbose_name='照片') #改为imageField可以处理图像,
#改为ImageField必须下载依赖库, pip install Pillow
subject = models.ForeignKey(to=Subject, on_delete=models.DO_NOTHING, db_column='subject_no')
# DO_NOTHING这个参数意思是删除学科根据数据库设定是否删除老师
#db_column='subject_no' 这个是指定对应数据库那个字段
class Meta:
managed = False
db_table = 'tb_teachers'
verbose_name = '老师'
verbose_name_plural = '老师'
自定义模型显示方式
在admin.py中
#在之前修改上新加了老师类
from django.contrib import admin
from polls.models import Subject,Teachers
class SubjectModelAdmin(admin.ModelAdmin):
list_display = ('no','name','intro','is_hot')
search_fields = ('name',)
ordering = ('no',) #subject学科的显示方式,上文中有详解
class TeachersModelAdmin(admin.ModelAdmin): #添加这个
list_display = ('no','name','sex','birth','good_count','bad_count','subject')
search_fields = ('name',)
ordering = ('no',) #teachaers老师的显示方式
admin.site.register(Subject,SubjectModelAdmin)
admin.site.register(Teachers,TeachersModelAdmin) #映射到后台
在后台手动添加外键项时不方便
在models.py中Subject类添加__str__方法,后台要调用这个类就会返回这个类的名称属性
def __str__(self):
return self.name
-- 再次刷新后台添加外键数据的时候就显示中文
在views.py中修改teachers函数
def show_teacher(request: HttpResponse) ->HttpResponse:
try:
no = int(request.GET.get('sno',1))
#print(no)
subject = Subject.objects.only('name').get(no=no) #根据传入的sno值查找学科名称,不加only也可以
teachers = Teacher.objects.filter(sno=no).order_by('no') #根据传入的no值,然后根据外键查询老师
return render(request,'teachers.html',{'teachers': teachers,'subjects':subject})
except (ValueError,Subject.DoesNotExist):
return redirect('/') #页面重定向
修改teachers.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>老师信息</title>
<style>
#container {
width: 80%;
margin: 10px auto;
}
.teacher {
width: 100%;
margin: 0 auto;
padding: 10px 0;
border-bottom: 1px dashed gray;
overflow: auto;
}
.teacher div {
float: left;
}
.photo {
height: 140px;
border-radius: 75px;
overflow: hidden;
margin-left: 20px;
}
.info {
width: 75%;
margin-left: 30px;
}
.info div {
clear: both;
margin: 5px 10px;
}
.info span {
margin-right: 25px;
}
.info a {
text-decoration: none;
color: darkcyan;
}
.user {
float: right;
margin: 10px 10px;
}
.user+h1 {
clear: both;
}
#photo {
border-radius: 32px 32px 32px 32px;
}
</style>
</head>
<body>
<div id="container">
<hr>
{% if not teachers %}
<h2>暂无该老师的信息</h2>
{% endif %}
<h1>{{ subjects.name }}学科的老师</h1>
<hr>
{% for teacher in teachers %}
<div class="teacher">
<div class="photo">
<img src="static/images/{{ teacher.photo }}" height="140" alt="">
</div>
<div class="info">
<div>
<span><strong>姓名:{{ teacher.name }}</strong></span>
<span>性别:{{ teacher.sex | yesno:'男,女' }}</span>
<span>出生日期:{{ teacher.birth }}</span>
</div>
<div class="intro">{{ teacher.intro }}</div>
<div class="comment">
<a href="">好评</a> (<strong>{{ teacher.gcount }}</strong>)
<a href="">差评</a> (<strong>{{ teacher.bcount }}</strong>)
</div>
</div>
</div>
{% endfor %}
<a href="/">返回首页</a> #返回'/'url
</div>
</body>
</html>