Skip to content

Commit

Permalink
更新了部分文档
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrued committed Jun 26, 2020
1 parent 60587f0 commit 0460425
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 283 deletions.
254 changes: 217 additions & 37 deletions Day41-55/42.深入模型.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,29 +268,6 @@ Teacher.objects.filter(subject__name__contains='全栈')
> **说明3**:如果希望更新多条数据,不用先逐一获取模型对象再修改对象属性,可以直接使用QuerySet对象的`update()`方法一次性更新多条数据。
1. Django框架本身有自带的数据模型,我们稍后会用到这些模型,为此我们先做一次迁移操作。所谓迁移,就是根据模型自动生成关系数据库中的二维表,命令如下所示:

```Shell
(venv)$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
```


### 利用Django后台管理模型

Expand All @@ -312,11 +289,11 @@ Teacher.objects.filter(subject__name__contains='全栈')
3. 运行项目,在浏览器中访问`http://127.0.0.1:8000/admin`,输入刚才创建的超级用户账号和密码进行登录。

![](./res/django-admin-login.png)
![](/Users/Hao/Desktop/Python-100-Days/Day41-55/res/django-admin-login.png)

登录后进入管理员操作平台。

![](./res/django-admin-apps.png)
![](res/django-admin-apps.png)

注意,我们暂时还没能在`admin`应用中看到之前创建的模型类,为此需要在`polls`应用的`admin.py`文件中对需要管理的模型进行注册。

Expand All @@ -325,9 +302,7 @@ Teacher.objects.filter(subject__name__contains='全栈')
```Python
from django.contrib import admin

```

from polls.models import Subject, Teacher
from polls.models import Subject, Teacher

admin.site.register(Subject)
admin.site.register(Teacher)
Expand All @@ -343,29 +318,234 @@ from polls.models import Subject, Teacher

- 添加学科。

![](./res/django-admin-create.png)
![](res/django-admin-add-model.png)

- 查看所有学科。

![](./res/django-admin-read.png)
![](res/django-admin-view-models.png)

- 删除和更新学科。

![](./res/django-admin-delete-update.png)
![](res/django-admin-delete-update-model.png)

6. 注册模型管理类。

可能大家已经注意到了,刚才在后台查看部门信息的时候,显示的部门信息并不直观,为此我们再修改`admin.py`文件,通过注册模型管理类,可以在后台管理系统中更好的管理模型。

```Python
from django.contrib import admin

from polls.models import Subject, Teacher

```

![](./res/django-admin-models-detail.png)

为了更好的查看模型数据,可以为`Subject``Teacher`两个模型类添加`__str__`魔法方法。修改代码后的效果如下图所示。

![](./res/django-amdin-models-detail-modified.png)
class SubjectModelAdmin(admin.ModelAdmin):
list_display = ('no', 'name', 'intro', 'is_hot')
search_fields = ('name', )
ordering = ('no', )


class TeacherModelAdmin(admin.ModelAdmin):
list_display = ('no', 'name', 'sex', 'birth', 'good_count', 'bad_count', 'subject')
search_fields = ('name', )
ordering = ('no', )


admin.site.register(Subject, SubjectModelAdmin)
admin.site.register(Teacher, TeacherModelAdmin)
```

![](res/django-admin-view-models-subject.png)

![](res/django-admin-view-models-teacher.png)

为了更好的查看模型,我们为`Subject`类添加`__str__`魔法方法,并在该方法中返回学科名字。这样在如上图所示的查看老师的页面上显示老师所属学科时,就不再是`Subject object(1)`这样晦涩的信息,而是学科的名称。

### 实现学科页和老师页效果

1. 修改`polls/views.py`文件,编写视图函数实现对学科页和老师页的渲染。

```Python
from django.shortcuts import render, redirect

from polls.models import Subject, Teacher


def show_subjects(request):
subjects = Subject.objects.all().order_by('no')
return render(request, 'subjects.html', {'subjects': subjects})


def show_teachers(request):
try:
sno = int(request.GET.get('sno'))
teachers = []
if sno:
subject = Subject.objects.only('name').get(no=sno)
teachers = Teacher.objects.filter(subject=subject).order_by('no')
return render(request, 'teachers.html', {
'subject': subject,
'teachers': teachers
})
except (ValueError, Subject.DoesNotExist):
return redirect('/')
```

2. 修改`templates/subjects.html``templates/teachers.html`模板页。

`subjects.html`

```HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学科信息</title>
<style>
#container {
width: 80%;
margin: 10px auto;
}
.user {
float: right;
margin-right: 10px;
}
.user>a {
margin-right: 10px;
}
#main>dl>dt {
font-size: 1.5em;
font-weight: bold;
}
#main>dl>dd {
font-size: 1.2em;
}
a {
text-decoration: none;
color: darkcyan;
}
</style>
</head>
<body>
<div id="container">
<div class="user">
<a href="login.html">用户登录</a>
<a href="register.html">快速注册</a>
</div>
<h1>扣丁学堂所有学科</h1>
<hr>
<div id="main">
{% for subject in subjects %}
<dl>
<dt>
<a href="/teachers/?sno={{ subject.no }}">{{ subject.name }}</a>
{% if subject.is_hot %}
<img src="/static/images/hot-icon-small.png">
{% endif %}
</dt>
<dd>{{ subject.intro }}</dd>
</dl>
{% endfor %}
</div>
</div>
</body>
</html>
```

`teachers.html`

```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;
}
</style>
</head>
<body>
<div id="container">
<h1>{{ subject.name }}学科的老师信息</h1>
<hr>
{% if not teachers %}
<h2>暂无该学科老师信息</h2>
{% endif %}
{% 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>&nbsp;(<strong>{{ teacher.good_count }}</strong>)
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="">差评</a>&nbsp;<strong>{{ teacher.bad_count }}</strong>)
</div>
</div>
</div>
{% endfor %}
<a href="/">返回首页</a>
</div>
</body>
</html>
```

3. 修改`vote/urls.py`文件,实现映射URL

```Python
from django.contrib import admin
from django.urls import path

from polls.views import show_subjects, show_teachers

urlpatterns = [
path('admin/', admin.site.urls),
path('', show_subjects),
path('teachers/', show_teachers),
]
```

到此为止,页面上需要的图片(静态资源)还没有能够正常展示,我们在下一章节中为大家介绍如何处理模板页上的需要的静态资源。

### 补充内容

Expand Down
Loading

0 comments on commit 0460425

Please sign in to comment.