## 【博主推薦】1元學習《Django零基礎到項目實戰(zhàn)》視頻教程:http://dwz.cn/VHRVxWfC
演示地址: http://mxonline.mtianyan.cn
教程倉庫地址1: https://github.com/mtianyan/DjangoGetStarted
教程倉庫地址2: https://github.com/mtianyan/Mxonline2
教程倉庫地址3: https://github.com/mtianyan/Mxonline3
## 7-1 django templates模板繼承1
- 機構可以篩選類別
- 機構可以根據(jù)所在地區(qū)進行分類
右側我要學習功能: form表單提交
右下:授課機構排名
頁面頭部與底部為全局頭和全局底部。
### Django template 共用頭部底部機制
將head和foot放在兩個html中盾鳞,然后在寫其他需要這兩個部分的頁面時include進來犬性。
Django也是支持include機制的。
### include的問題
include的進來的死頁面雁仲,這時候該怎么辦仔夺?
解決這種問題:進行模板的繼承機制。定義一個父類的框架攒砖,子類可以替換其中一部分block,子類只需要重寫自己需要改變的block日裙。
### template中新建base.html
將課程機構列表頁吹艇。orglist拷貝進template目錄
將orglist內容替換base內容。
http://upload-images.jianshu.io/upload_images/1779926-0269443b69f80570.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240
將div收起來

loadstaticfiles & 修改靜態(tài)文件路徑為static
>這個步驟做過太多遍了昂拂,自行完成受神。耐心就行了。
### 定義父模板: 包含head & footer
title應該是可以被子頁面替換的所以要包起來格侯。

css有共用的部分鼻听,也有可以被子頁面替換的部分财著。

js同理

面包屑是需要被各個頁面自己替換的。

將正文內容包起來撑碴;

此時base頁面就制作好了
## 7-2 開始orglist編寫
第一步:清空所有內容
- 繼承base頁面

- 覆蓋父類的title

- 書寫課程機構view
organization/views.py
```
# encoding: utf-8
from django.views.generic.base import View
# 處理課程機構列表的view
class OrgView(View):
? ? def get(self,request):
? ? ? ? return render(request, "org-list.html", { })
```
- Django2.0.1配置課程機構首頁url
```
? ? # 課程機構首頁url
? ? path('org_list/', OrgView.as_view(), name="org_list"),
```
- Django1.9.8配置url:
```
? ? # 課程機構首頁url
? ? url(r'^org_list/$', OrgView.as_view(), name="org_list"),
```
### 修改面包屑
- base中只保留首頁
- org中重寫block custom_bread
- block之間沒有先后順序撑教。
- 將base中block content拿到orglist重寫

- 然后將base中block中間section刪除掉

>orglist開始loadstaticfiles
`ctrl+d`快速刪除

頁面的繼承關系使得變量也可以直接用
>比如user中的form數(shù)據(jù)傳遞到register文件當中.如果register繼承的是base頁面。
base頁面當中也是可以用這些數(shù)據(jù)的醉拓。`參數(shù)的向上傳遞`
每個request對象都會傳遞到html中來伟姐,如果繼承了base,request也會向上傳遞到base亿卤。
base中就可以加入我們的邏輯: 用戶是否登錄等愤兵。
小節(jié)結束對應commit:
>完成Django templates的繼承關系了解,機構列表展示頁排吴。對應7-1 & 2
## 7-3 課程機構列表頁數(shù)據(jù)展示1
確定由后臺傳過來的動態(tài)數(shù)據(jù):
授課機構列表本身秆乳, 授課機構的排名,所在地區(qū)(后臺取出所有地區(qū)), 機構類別寫成靜態(tài)钻哩,因為一般不怎么變動矫夷。
在xadmin中添加城市信息,課程信息憋槐。
添加城市

添加機構双藕。
插播知識點:

這里指定的路徑是一個相對路徑
setting中要配置我們把文件存放在哪個根目錄之下
```
# 設置我們上傳文件的路徑
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
```
在項目根目錄創(chuàng)建media文件夾
在后臺上傳圖片


修改機構信息中封面圖為logo
自行添加十個課程機構
### models中添加機構類別
organization/models.py:
```python
class CourseOrg(models.Model):
? ? ORG_CHOICES =(
? ? ? ? ("pxjg", u"培訓機構"),
? ? ? ? ("gx", u"高校"),
? ? ? ? ("gr", u"個人"),
? ? )
? ? name = models.CharField(max_length=50, verbose_name=u"機構名稱")
? ? # 機構描述,后面會替換為富文本展示
? ? desc = models.TextField(verbose_name=u"機構描述")
? ? # 機構類別:
? ? category = models.CharField(max_length=20, choices=ORG_CHOICES, verbose_name=u"機構類別", default="pxjg")
```
修改了models之后做數(shù)據(jù)庫的變動:
```python
makemigrations organization
migrate organization
```

完成之后打開Navicat進行驗證:

可以看到新增了阳仔。
### 完善我們的view
將列表里的靜態(tài)數(shù)據(jù)變成后臺獲取的動態(tài)數(shù)據(jù)
organization/views.py
```python
from .models import CourseOrg, CityDict
class OrgView(View):
? ? def get(self,request):
? ? ? ? # 查找到所有的課程機構
? ? ? ? all_orgs = CourseOrg.objects.all()
? ? ? ? # 取出所有的城市
? ? ? ? all_citys = CityDict.objects.all()
? ? ? ? return render(request, "org-list.html", {
? ? ? ? ? ? "all_orgs":all_orgs,
? ? ? ? ? ? "all_citys": all_citys,
? ? ? ? })
```