@@ -268,29 +268,6 @@ Teacher.objects.filter(subject__name__contains='全栈')
268268
269269> ** 说明3** :如果希望更新多条数据,不用先逐一获取模型对象再修改对象属性,可以直接使用QuerySet对象的` update() ` 方法一次性更新多条数据。
270270
271- 1 . Django框架本身有自带的数据模型,我们稍后会用到这些模型,为此我们先做一次迁移操作。所谓迁移,就是根据模型自动生成关系数据库中的二维表,命令如下所示:
272-
273- ``` Shell
274- (venv)$ python manage.py migrate
275- Operations to perform:
276- Apply all migrations: admin, auth, contenttypes, sessions
277- Running migrations:
278- Applying contenttypes.0001_initial... OK
279- Applying auth.0001_initial... OK
280- Applying admin.0001_initial... OK
281- Applying admin.0002_logentry_remove_auto_add... OK
282- Applying contenttypes.0002_remove_content_type_name... OK
283- Applying auth.0002_alter_permission_name_max_length... OK
284- Applying auth.0003_alter_user_email_max_length... OK
285- Applying auth.0004_alter_user_username_opts... OK
286- Applying auth.0005_alter_user_last_login_null... OK
287- Applying auth.0006_require_contenttypes_0002... OK
288- Applying auth.0007_alter_validators_add_error_messages... OK
289- Applying auth.0008_alter_user_username_max_length... OK
290- Applying auth.0009_alter_user_last_name_max_length... OK
291- Applying sessions.0001_initial... OK
292- ```
293-
294271
295272### 利用Django后台管理模型
296273
@@ -312,11 +289,11 @@ Teacher.objects.filter(subject__name__contains='全栈')
312289
3132903 . 运行项目,在浏览器中访问` http://127.0.0.1:8000/admin ` ,输入刚才创建的超级用户账号和密码进行登录。
314291
315- ![ ] ( . /res/django-admin-login.png)
292+ ![ ] ( /Users/Hao/Desktop/Python-100-Days/Day41-55 /res/django-admin-login.png)
316293
317294 登录后进入管理员操作平台。
318295
319- ![ ] ( ./ res/django-admin-apps.png)
296+ ![ ] ( res/django-admin-apps.png )
320297
321298 注意,我们暂时还没能在` admin ` 应用中看到之前创建的模型类,为此需要在` polls ` 应用的` admin.py ` 文件中对需要管理的模型进行注册。
322299
@@ -325,9 +302,7 @@ Teacher.objects.filter(subject__name__contains='全栈')
325302 ``` Python
326303 from django.contrib import admin
327304
328- ```
329-
330- from polls.models import Subject, Teacher
305+ from polls.models import Subject, Teacher
331306
332307 admin.site.register(Subject)
333308 admin.site.register(Teacher)
@@ -343,29 +318,234 @@ from polls.models import Subject, Teacher
343318
344319 - 添加学科。
345320
346- 
321+ ![ ] ( res/django-admin-add-model .png )
347322
348323 - 查看所有学科。
349324
350- 
325+ ![ ] ( res/django-admin-view-models .png )
351326
352327 - 删除和更新学科。
353328
354- 
329+ ![ ] ( res/django-admin-delete-update-model .png )
355330
3563316 . 注册模型管理类。
357332
358333 可能大家已经注意到了,刚才在后台查看部门信息的时候,显示的部门信息并不直观,为此我们再修改` admin.py ` 文件,通过注册模型管理类,可以在后台管理系统中更好的管理模型。
359334
360335 ``` Python
336+ from django.contrib import admin
337+
338+ from polls.models import Subject, Teacher
361339
362- ```
363-
364- ![ ] ( ./res/django-admin-models-detail.png )
365-
366- 为了更好的查看模型数据,可以为` Subject ` 和` Teacher ` 两个模型类添加` __str__ ` 魔法方法。修改代码后的效果如下图所示。
367340
368- ![ ] ( ./res/django-amdin-models-detail-modified.png )
341+ class SubjectModelAdmin (admin .ModelAdmin ):
342+ list_display = (' no' , ' name' , ' intro' , ' is_hot' )
343+ search_fields = (' name' , )
344+ ordering = (' no' , )
345+
346+
347+ class TeacherModelAdmin (admin .ModelAdmin ):
348+ list_display = (' no' , ' name' , ' sex' , ' birth' , ' good_count' , ' bad_count' , ' subject' )
349+ search_fields = (' name' , )
350+ ordering = (' no' , )
351+
352+
353+ admin.site.register(Subject, SubjectModelAdmin)
354+ admin.site.register(Teacher, TeacherModelAdmin)
355+ ```
356+
357+ ![ ] ( res/django-admin-view-models-subject.png )
358+
359+ ![ ] ( res/django-admin-view-models-teacher.png )
360+
361+ 为了更好的查看模型,我们为` Subject ` 类添加` __str__ ` 魔法方法,并在该方法中返回学科名字。这样在如上图所示的查看老师的页面上显示老师所属学科时,就不再是` Subject object(1) ` 这样晦涩的信息,而是学科的名称。
362+
363+ ### 实现学科页和老师页效果
364+
365+ 1 . 修改` polls/views.py ` 文件,编写视图函数实现对学科页和老师页的渲染。
366+
367+ ``` Python
368+ from django.shortcuts import render, redirect
369+
370+ from polls.models import Subject, Teacher
371+
372+
373+ def show_subjects (request ):
374+ subjects = Subject.objects.all().order_by(' no' )
375+ return render(request, ' subjects.html' , {' subjects' : subjects})
376+
377+
378+ def show_teachers (request ):
379+ try :
380+ sno = int (request.GET .get(' sno' ))
381+ teachers = []
382+ if sno:
383+ subject = Subject.objects.only(' name' ).get(no = sno)
384+ teachers = Teacher.objects.filter(subject = subject).order_by(' no' )
385+ return render(request, ' teachers.html' , {
386+ ' subject' : subject,
387+ ' teachers' : teachers
388+ })
389+ except (ValueError , Subject.DoesNotExist):
390+ return redirect(' /' )
391+ ```
392+
393+ 2 . 修改`templates/ subjects.html` 和`templates/ teachers.html` 模板页。
394+
395+ `subjects.html`
396+
397+ ```HTML
398+ < ! DOCTYPE html>
399+ < html lang= " en" >
400+ < head>
401+ < meta charset= " UTF-8" >
402+ < title> 学科信息< / title>
403+ < style>
404+ # container {
405+ width: 80 % ;
406+ margin: 10px auto;
407+ }
408+ .user {
409+ float : right;
410+ margin- right: 10px ;
411+ }
412+ .user> a {
413+ margin- right: 10px ;
414+ }
415+ # main>dl>dt {
416+ font- size: 1. 5em ;
417+ font- weight: bold;
418+ }
419+ # main>dl>dd {
420+ font- size: 1. 2em ;
421+ }
422+ a {
423+ text- decoration: none;
424+ color: darkcyan;
425+ }
426+ < / style>
427+ < / head>
428+ < body>
429+ < div id = " container" >
430+ < div class = " user" >
431+ < a href= " login.html" > 用户登录< / a>
432+ < a href= " register.html" > 快速注册< / a>
433+ < / div>
434+ < h1> 扣丁学堂所有学科< / h1>
435+ < hr>
436+ < div id = " main" >
437+ {% for subject in subjects % }
438+ < dl>
439+ < dt>
440+ < a href= " /teachers/?sno={{ subject.no }} " > {{ subject.name }}< / a>
441+ {% if subject.is_hot % }
442+ < img src= " /static/images/hot-icon-small.png" >
443+ {% endif % }
444+ < / dt>
445+ < dd> {{ subject.intro }}< / dd>
446+ < / dl>
447+ {% endfor % }
448+ < / div>
449+ < / div>
450+ < / body>
451+ < / html>
452+ ```
453+
454+ `teachers.html`
455+
456+ ```HTML
457+ < ! DOCTYPE html>
458+ < html lang= " en" >
459+ < head>
460+ < meta charset= " UTF-8" >
461+ < title> 老师信息< / title>
462+ < style>
463+ # container {
464+ width: 80 % ;
465+ margin: 10px auto;
466+ }
467+ .teacher {
468+ width: 100 % ;
469+ margin: 0 auto;
470+ padding: 10px 0 ;
471+ border- bottom: 1px dashed gray;
472+ overflow: auto;
473+ }
474+ .teacher> div {
475+ float : left;
476+ }
477+ .photo {
478+ height: 140px ;
479+ border- radius: 75px ;
480+ overflow: hidden;
481+ margin- left: 20px ;
482+ }
483+ .info {
484+ width: 75 % ;
485+ margin- left: 30px ;
486+ }
487+ .info div {
488+ clear: both;
489+ margin: 5px 10px ;
490+ }
491+ .info span {
492+ margin- right: 25px ;
493+ }
494+ .info a {
495+ text- decoration: none;
496+ color: darkcyan;
497+ }
498+ < / style>
499+ < / head>
500+ < body>
501+ < div id = " container" >
502+ < h1> {{ subject.name }}学科的老师信息< / h1>
503+ < hr>
504+ {% if not teachers % }
505+ < h2> 暂无该学科老师信息< / h2>
506+ {% endif % }
507+ {% for teacher in teachers % }
508+ < div class = " teacher" >
509+ < div class = " photo" >
510+ < img src= " /static/images/{{ teacher.photo }} " height= " 140" alt= " " >
511+ < / div>
512+ < div class = " info" >
513+ < div>
514+ < span>< strong> 姓名:{{ teacher.name }}< / strong>< / span>
515+ < span> 性别:{{ teacher.sex | yesno:' 男,女' }}< / span>
516+ < span> 出生日期:{{ teacher.birth }}< / span>
517+ < / div>
518+ < div class = " intro" > {{ teacher.intro }}< / div>
519+ < div class = " comment" >
520+ < a href= " " > 好评< / a> & nbsp;(< strong> {{ teacher.good_count }}< / strong> )
521+ & nbsp;& nbsp;& nbsp;& nbsp;
522+ < a href= " " > 差评< / a> & nbsp;< strong> {{ teacher.bad_count }}< / strong> )
523+ < / div>
524+ < / div>
525+ < / div>
526+ {% endfor % }
527+ < a href= " /" > 返回首页< / a>
528+ < / div>
529+ < / body>
530+ < / html>
531+ ```
532+
533+ 3 . 修改`vote/ urls.py` 文件,实现映射URL 。
534+
535+ ```Python
536+ from django.contrib import admin
537+ from django.urls import path
538+
539+ from polls.views import show_subjects, show_teachers
540+
541+ urlpatterns = [
542+ path(' admin/' , admin.site.urls),
543+ path(' ' , show_subjects),
544+ path(' teachers/' , show_teachers),
545+ ]
546+ ```
547+
548+ 到此为止,页面上需要的图片(静态资源)还没有能够正常展示,我们在下一章节中为大家介绍如何处理模板页上的需要的静态资源。
369549
370550# ## 补充内容
371551
0 commit comments