首先說明css
本資源來源是幕課網的python教育平臺,可是原教程是python 2.7 django是1.9.8 如今學起來太過期了,因此我想把他 整理修改爲高版本,可是時間有限,我就沒有 錄課,只作了代碼從新打一片,修了版本差別,從新實現,另外原教程並無免費提供代碼,我將代碼整理出,方便python的人學習 html
本次環境是2018年最新pytho 3.6以上 django 版本 2.1以上 在次註定版本差別問題 前端
其實本人工做給django無關,只是別人分享給我了這套視頻,放在硬盤也沒有用,刪掉惋惜,因此決定把他成文章,而後就能夠安心清楚硬盤,,python
正文:mysql
第一章,,先來複習之前的 django 知識, 由於這兒畢竟是實戰課程,因此不會作基礎說明,可是仍是小小的複習一下基礎,熱身開始!!!! git
在寫一個項目前先搞一個aPPgithub
Tools->Run manage Task....sql
> startapp message #運行此命令新建一條app數據庫
因爲一個項目app文件太多,不方便管理,能夠新建一個apps總文件,把全部的app文件放入apps文件中,改變了app路徑,那麼不管是導入仍是IDE提示都會出錯,在導入時須要:from apps.message import views,//django
解決ide識別問題能夠,file ->meki file->
解決文件路徑問題能夠在配置文件中加入:
配置靜態文靜:先新建一個static文件:並在steetigs.py中配置文件中添加:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
新建日誌生成文件:新建 名爲log文件。。。。。
新建上傳文件,新建 名爲media。。。。。
配置數據庫:因爲數據庫訪問權限是很嚴管的,這不是正式開發,只是本機測試,暫沒有建用戶,和訪問ip,這兒只做簡單配置
在正式使用數庫以前,須要安裝mysql驅動
> pip install pymysql #執行這條指令能夠安裝驅動
其實不安裝數據庫,django默認也有數據庫可使用,只是mysql太流行了,做爲學習者,不使用說不過去。。。逼着就 上吧!!
安裝好後,配置文件修改數據庫:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "mxshop",
'USER':'root',
'PASSWORD':'142857',
'HOST':"127.0.0.1",
'PORT': '3306',
"OPTIONS":{"init_command":"SET default_storage_engine=INNODB;"}
}
}
最後一步還要在主app的 —init__.py 中加入:
import pymysql
pymysql.install_as_MySQLdb()
配置好數據庫後須要同步數據庫自動生成表
Tools->Run mange->
makemigrations
//正常返回: No chanage detected
Tools->Run mange->
migrate
//生成數據庫表。
以上配置後能夠開始寫路由了,先在url.py中,增長路由一個新的路由,和一個對應函數,url.py 中增長路由,,views.py文件中增長函數對應
路由部份
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',getform)
]
views.py文件中部份
def getform(request):
return render(render,'Xform.html') //不能直接寫form.html 注意關健字
本章演示請求到顯示過程:
1,先在模板文件建一個帶表單html的文件:
簡要代碼以下:
<link rel="stylesheet" href="/static/css/style.css"> <form action="" method="post" class="smart"> <h1>留言信息 <span>請填寫留言信息</span> </h1> <label for=""> <spqn>姓名</spqn> <input id="name" type="text" name="name" class=""> <div class = ""></div> </label> <label for=""> <spqn></spqn> <input id="bnv" type="submit" value="提交" class=""> <div class = ""></div> </label>
2,在靜態文件下面新建一個 css,js,的文件,放入css文件
前端內容先跳過:
如今開始討論 OPm操做
在djagnog的app中,會一個models.py文件,在裏面能夠操做數據庫模型,每一類對應一張表
好比如今新建一個表,並設置字段,,
class UserMessage(models.Model): name = models.CharField(max_length=20,verbose_name="用戶名") email = models.CharField(max_length=25, verbose_name="郵箱") address = models.CharField(max_length=100, verbose_name="聯繫地址") mess = models.CharField(max_length=200,verbose_name="留言信息") class Meta: verbose_name= "用 戶信息" verbose_name_plural = verbose_name
每建 一個app 須要在配置文件中加放註冊:在
INSTALLED_APPS=[
..........................
加入app名字。這兒以dessage爲例
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'message' , ]
在次運行同步表,則生成一個UserMessage的表
在上面只是用了 CharFied字段,,在django的有很是多的屬性字段,對於基礎不熟於請參考原基礎,這兒我是整理,僅限本人能看懂
models.ForeignKey //主健和外健
models.DateTimeField //
models.IntegerField
models.IPAddressField
models.FileField
models.ImageField
有了表列屬性,還得有字段屬性,好比maxa_lenagt 這類的
還有mete的屬性 好比
Class Meta:
verbose_name = "用戶留言"
db_table = "表名字"
ordeing = "_object_id" //排序方式
verbose_name _plurdl = verbose_name 僅用於顯示,後臺用
數據庫操做:
object.all是查的方法,返回query類型 ,能夠遍歷其元素
def getform(request):
all_message = UserMessage.objects.all()
for message in messages:
pint message.name
return render(request,'message_form.html')
語法示例:
1
all_message = UserMessage.objects.filter(name = 'bobby')
2
all_message = UserMessage.objects.filter(name = 'bobby',address = '不海')
讀取返的數據的方法能夠用all_message[0],也能夠切片讀取【0:6】
增長數據示例:
def getform(request):
user_m = userMessage() //這個類一旦運行,就會實例化一個句柄給對象,就能夠給字段估值
user_M.name = 'bobby2'
user_m.message = "hekllo"
user_address.address = "上海"
user_m.email = "2@2.com"
user_m.object_id = "123456"
user_m.save() // 調用這個方法就能夠保存了
return render(request,'message_form.html')
談談怎麼接收數據!!前端數據是經過from name = "?????"
後臺不能直接接收,須要在設置文件中註釋掉 csrf相關信息 或帶上csrs信息發送 表單中加一下
{%csrf token%}
模塊嵌套
後臺獲得的數據先放放一個字典,無組,對象都行,在視圖函數中傳入,前端就能夠接收到例;
def getform(request):
all_me = UserMessage.objects.filter(name="xsy")
if all_me:
message = all_me
return render(request,'message_form.html',{"mess":all_me})
--------------------------------------------------------------------------------------
如下是數據庫設計,包括表繼承。步驟,主要是熟悉字段用,理一下而己
新建5個app 並在models.py中建好類,用於生成表,和後臺管理用
第一個 users 這個表繼承系統的用戶表,代碼以下:
class UserProfile(AbstractUser):
genders =(
("meae","男"),
("female","女"),
)
nick_name = models.CharField(max_length=50,verbose_name = "稱呢",default = "")
birday = models.DateTimeField(verbose_name = "生日",null = "",bland="")
gender = models.CharField(choices=genders,default="female",max_length=6,verbose_name="性別")
addfress = models.CharField(max_length=100,default = "",verbose_name="地")
mobile = models.CharField(max_length=11,null=True,bland=True,verbose_name="手機號")
image = models.ImageField(upload_to="image/%y/%m",default="image/defult.png",max_length=100)
class Meta:
verbose_name = "用戶信息"
verbose_name_plural = verbose_name
def __str__(self):
return self.username //重載類的一個字符方法而己
而後在apps 那兒註冊models 相關的app 在設置模塊那兒找到如下代碼,在最後加入:users,並在下面加入重載方法:
AUTH_USER_MODEL = "users.UserProfile"
NSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users'
]
AUTH_USER_MODEL = "users.UserProfile"
由於咱們要操做圖片庫,因此要安裝pillow 處理圖片的模塊,因此在這兒先運行 pip install Pillow
最後在執行下面二個命令就新建好一個對應表了,其它表也是過程同樣,,說要說明的是,若是,這個suers模型就改名,或者操做錯了,造 成
表混亂,刪除所有在在執行一次好。。
python manage.py makemigrations
python manage.py migrate
咱們設設計一個用戶app 主要放輪播圖,用戶資料 和驗證信息用,
models.py文件內容以下:
from django.db import models from datetime import datetime # Create your models here. from django.contrib.auth.models import AbstractUser class UserProfile(AbstractUser): genders =( ("meae","男"), ("female","女"), ) nick_name = models.CharField(max_length=50,verbose_name = "稱呢",default = "") birday = models.DateTimeField(verbose_name = "生日",null = "",blank="") gender = models.CharField(choices=genders,default="female",max_length=6,verbose_name="性別") addfress = models.CharField(max_length=100,default = "",verbose_name="地") mobile = models.CharField(max_length=11,null=True,blank=True,verbose_name="手機號") image = models.ImageField(upload_to="image/%y/%m",default="image/defult.png",max_length=100) class Meta: verbose_name = "用戶信息" verbose_name_plural = verbose_name def __str__(self): return self.username class EmailVerifyRecord(models.Model): code = models.CharField(max_length=20,verbose_name="驗證碼") email = models.EmailField(max_length=50,verbose_name="郵箱") send_type = models.CharField(choices=(("register","註冊"),("forget","女")),defalut = "forget") send_time = models.DateTimeField(default="datetime.now") class Meta: vebose_name = "郵箱驗證碼" verbose_name_plural = vebose_name class Banner(models.Model): title = models.CharField(max_length=100,verbose_name="標題") image = models.ImageField(upload_to="banner/%Y/%m",verbose_name="輪播圖") url = models.URLField(max_length=200,verbose_name="訪問地址") index = models.IntegerField(default = 100,verbose_name="排序") add_time = models.DateTimeField(datetime = datetime.now,verbose_name="添加時間") class Meta: verbose_name = "輪播圖" verbose_name_plural = verbose_name
在建一個管理課程app,包括課程資源,章節名稱,課程名稱 courses
models.py文件內容以下:
from django.db import models from datetime import datetime # Create your models here. class Course(models.Model): name = models.CharField(max_length=50,verbose_name="課程名") desc = models.CharField(max_length=300, verbose_name="課 程描述") detail = models.TextField(verbose_name="課程詳情") degree = models.CharField(choices=(('cj','初級'),('zj','中級'),('gj','高級')),default='cj',max_length=6) learn_times = models.IntegerField(default=0,verbose_name="學習時長") students = models.IntegerField(default=0,verbose_name="學習人數") fav_nums = models.IntegerField(default=0,verbose_name="收藏人數") image = models.ImageField(upload_to="courses/%Y/%m",default="",verbose_name="封面圖") click_nums = models.IntegerField(default=0,verbose_name = "點擊數") add_time = models.DateTimeField(default = datetime.now,verbose_name='添加時間') class Meta: verbose_name = "課程" verbose_name_plural = verbose_name class lesson(models.Model): course = models.ForeignKey(Course, verbose_name="課程",on_delete=models.CASCADE) name = models.CharField(max_length = 25,verbose_name="章節名" ) add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間") class Video(models.Model): lesson = models.ForeignKey(lesson, verbose_name="章節",on_delete=models.CASCADE) name = models.CharField(max_length=100,verbose_name="視頻名字") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加時間" ) class Meta: verbose_name = "章節資源" verbose_name_plural = "verbose_name" class CourseResouce(models.Model): name = models.CharField(max_length=100, verbose_name="資源名稱") download = models.FileField(upload_to="course/resourse/%Y/%m",verbose_name="資源文件",max_length = 100) add_time = models.DateTimeField(default= datetime.now, verbose_name="添加時間") class Meta: verbose_name = "課程資源" verbose_name_plural = "verbose_name"
在新建一個機構app 名爲: organization
models.py文件內容以下:
from django.db import models # Create your models here. from datetime import datetime from django.db import models from django.db import models class CityDict(models.Model): name = models.CharField(max_length = 20,verbose_name = "城市") desc = models.CharField(max_length=200,verbose_name="描述") add_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = "城市" verbose_name_plural = verbose_name class CourseOrg(models.Model): name = models.CharField(max_length=50,verbose_name="機構名稱") desc = models.TextField(verbose_name="機構描述") click_nums = models.IntegerField(default=0,verbose_name="點擊數'") fav_nums = models.ImageField(default=0,verbose_name="收藏數") image = models.ImageField(upload_to="org/%Y/%m",verbose_name="封面圖") address = models.CharField(max_length=150,verbose_name="機構地址") city = models.ForeignKey(CityDict,verbose_name="所在城市",on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = "課程機構" verbose_name_plural = verbose_name class Teacher(models.Model): org = models.ForeignKey(CourseOrg,verbose_name="所屬機構",on_delete=models.CASCADE) name = models.CharField(max_length = 50,verbose_name = "教師名字") work_years = models.IntegerField(default=0,verbose_name="工做年限") work_company = models.CharField(max_length=50,verbose_name="就任公司") work_position = models.CharField(max_length=50,verbose_name="公司職位") points = models.CharField(default=0,verbose_name="教育特色",max_length=100) fav_nume = models.IntegerField(default=0,verbose_name="點擊數") add_time = models.DateTimeField(default = datetime.now) class Meta: verbose_name = "教師" verbose_name_plural = verbose_name
在新建一個用戶課app 名爲 opration ,models.py的代碼以下:
from django.db import models from datetime import datetime from users.models import UserProfile from courses.models import Course # Create your models here. class userAsk(models.Model): name = models.CharField(max_length=20,verbose_name="姓名") mobile = models.CharField(max_length=11,verbose_name="手機") couse_name = models.CharField(max_length=50,verbose_name="課程名") add_time = models.DateTimeField(default=datetime,verbose_name="添加時間") class Mate: verbose_name = "用戶資詢" verbose_name_Plural = verbose_name class CourseComment(models.Model): user = models.ForeignKey(UserProfile,verbose_name="用戶",on_delete=models.CASCADE) course = models.ForeignKey(Course,verbose_name="課程",on_delete=models.CASCADE) Comment = models.CharField(max_length = 20, verbose_name = "評論") add_time = models.DateTimeField(default=datetime, verbose_name="添加時間") class Meta: verbose_name = "課程評論" verbose_name_plural = verbose_name class UserFavorite(models.Model): user = models.ForeignKey(UserProfile,verbose_name="用戶",on_delete=models.CASCADE) fav_id = models.IntegerField(default=0,verbose_name="數據id") fav_type = models.IntegerField(choices=((1,"課程"),(2,"課程機構"),(3,'教師名字')),default=1,verbose_name="收藏類型") add_time = models.DateTimeField(default=datetime, verbose_name="添加時間") class Meta: verbose_name = "用戶收藏" verbose_name_plural = verbose_name class UserMessage(models.Model): user = models.IntegerField(default=0,verbose_name="接收用戶") message = models.CharField(max_length = 200,verbose_name = "消息內容") has_read = models.BooleanField(default=False,verbose_name="不否也讀") add_time = models.DateTimeField(default=datetime, verbose_name="添加時間") class Meta: verbose_name = "用戶消息" verbose_name_plural = verbose_name class UserCourse(models.Model): user = models.ForeignKey(UserProfile, verbose_name="用戶",on_delete=models.CASCADE) course = models.ForeignKey(Course, verbose_name="課程",on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime, verbose_name="添加時間") class Meta: verbose_name = "用戶課程" verbose_name_plural = verbose_name
通過以上的構建 ,如今因此須要的app有和表基礎建好了,開始在設置那兒注刪全部的app 而後用於生成數據庫須要的表:
在主文件的setting.py 中找到:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'courses',
'organization',
'operation',
] //紅色部份是在後面添加全部的app名字
對於數據庫相關生在 操做最後就是生成數據庫表,和管理app文件過多的問題,新建一個apps文件,將因此app拖入,
而後執行:整個操做中,有外連的地方都要加上on_delete = models.CASCADE
python manage.py makemigrations
python manage.py migrate
塔建後臺管理系統:
默認 admin能夠登到後臺,可是賬號爲空,嘗試root訪問時出現:
因此得先createsuperuser 命令建立賬戶
Tools -> Run Mangae ..createsuperuser
登陸後臺是英文界面,修改中文方法:,將配置文件:
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
修改爲:#————————————————————————————————————————————————————————————————————————————————————————————-
以下:
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
重啓中文顯示了
#——————————————————————————————————————————————————————————————————————————————————————————————
如今咱們能夠把全部表註冊到後臺,原本user是自動注了,只是被們重載了,須要從新註冊過來
先作userProfile表
先找到user的app,打開 admin.py 文件
編寫代碼:
from django.contrib import admin
from .models import UserProfile
# Register your models here.
class UserProfileAdmin(admin.ModelAdmin):
pass
admin.site.register(UserProfile, UserProfileAdmin)
這樣便在後臺註冊一個表,
官方默認的管理系統很差看,咱們安裝一個Xadmin
在python3 .6 只 若是用pip install xAdmin的話會報編碼錯誤,估記是編碼問題
C:\Users\Administrator>workon Xflask
(xflask) C:\Users\Administrator>pip install xadmin
Collecting xadmin
Using cached https://files.pythonhosted.org/packages/1d/e9/2ac160c532d0d462142fa90aa86a0e317295e8d86fa4caf84d04f6e24c49/xadmin-0.6.1.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\ADMINI~1\AppData\Local\Temp\pip-install-ris4dywq\xadmin\setup.py", line 11, in <module>
long_description=open('README.rst').read(),
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 3444: illegal multibyte sequence
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\ADMINI~1\AppData\Local\Temp\pip-install-ris4dywq\xadmin\
只能用原碼下載方法,
使用:下載: https://github.com/sshwsfc/xadmin/tree/django2(記得要直接下載zip) 而後解壓放在根目錄,新建一個extera_apps,
專門外置組件,把下載好的xadmin放入,而後在配置settings.py文件中增長一條搜索路徑:紅包部份
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,os.path.join(BASE_DIR,'apps'))
sys.path.insert(0,os.path.join(BASE_DIR,'extra_apps'))
要想使用Xadmin 也是須要在setting.py 中 INSTALLED-APPS = []
那兒註冊一下的
添加
‘xadmin’,
'crispy_forms'
'reversion',
//這個是安裝xadmin時自動裝的,也要註冊一下,
最後運行python manage.py makemigrations 而後執行python manage.py migrate 生成數據庫
而後配置一下url ,,
rlpatterns = [
path('xadmin/', xadmin.site.urls),
]
做業??? 若是咱們是手工安裝的Xadmin 那兒依賴的包怎麼安裝呢,,回憶一下。。能夠用這個方法先安裝一下在刪掉,只要自動的依包就好:
pip install https://github.com/sshwsfc/xadmin/tarball/master
如今開始註冊後表。
因爲xadmin 須要一個adminx的模型 文件,因此咱們須要在每一個app建一個adminx.py文件而後註冊上內容:
以基本爲:
import xadmin
from .models import user //導入表
class EmailVerifyRecordAdmin(object):
list_display = ['code','email','send_type','send_time'] //用於顯示,每一個健表明一個字段名
search_fields =['code','email','send_type','send_time'] // 用於查詢用
list_filter = ['code','email','send_type','send_time'] // 用過過慮
xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin) // 生成註冊關聯
如今能作手就是在每一個app 下面,把每一個表都作一下關聯,,
第一先作關於用戶的表 users 的app 因爲user表也由系統生成,只作其它二張表:先建一個adminx。py 代碼以下
__author__ = "xsy" __date__ = "2018/10/25" import xadmin from .models import EmailVerifyRecord,Banner class EmailVerifyRecordAdmin(object): list_display = ['code','email','send_type','send_time'] search_fields =['code','email','send_type','send_time'] list_filter = ['code','email','send_type','send_time'] class BannerAdmin(object): list_display = ['title', 'image', 'url', 'index','add_time'] search_fields = ['title', 'image', 'url', 'index','add_time'] list_filter = ['title', 'image', 'url', 'index','add_time'] xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin) xadmin.site.register(Banner,BannerAdmin)
第二在作關於 教師的表 organization app 方法如上,先在目錄下建一個adminx.py 代碼以下:
__author__ = "xsy" __date__ = "2018/10/25" import xadmin from .models import CityDict, CourseOrg, Teacher class CityDictAdmin(object): list_display = ['name','desc','add_time'] search_fields = ['name','desc'] list_filter = ['name','desc','add_time'] class CourseOrgAdmin(object): list_display = ['name','desc','click_nums','fav_nums','image','address','city','add_time'] search_fields = ['name','desc','click_nums','fav_nums','image','address','city'] list_filter = ['name','desc','click_nums','fav_nums','image','address','city','add_time'] class TeacherAdmin(object): list_display = ['org', 'name', 'work_years', 'work_company', 'work_position', 'points', 'fav_nume', 'add_time'] search_fields = ['org', 'name', 'work_years', 'work_company', 'work_position', 'points', 'fav_nume'] list_filter = ['org', 'name', 'work_years', 'work_company', 'work_position', 'points', 'fav_nume', 'add_time'] xadmin.site.register(Teacher,TeacherAdmin) xadmin.site.register(CityDict,CityDictAdmin) xadmin.site.register(CourseOrg,CourseOrgAdmin)
第三,在用戶操做的 operation app 關聯,代碼以下:
__author__ = "xsy" __date__ = "2018/10/25" import xadmin from .models import userAsk, UserCourse, UserMessage, CourseComment, UserFavorite class userAsKAdmin(object): list_display = ['name', 'mobile', 'couse_name', 'add_time'] search_field =['name', 'mobile', 'couse_name',] list_filter = ['name', 'mobile', 'couse_name', 'add_time'] class UserCourseAdmin(object): list_display = ['user', 'course', 'add_time'] search_field = ['user', 'course'] list_filter = ['user', 'course', 'add_time'] class UserMessageAdmin(object): list_display = ['user', 'message', 'has_read','add_time'] search_field = ['user', 'message'] list_filter = ['user', 'message', 'has_read','add_time'] class CourseCommentAdmin(object): list_display = ['user', 'course', 'Comment', 'add_time'] search_field = ['user', 'course', 'Comment'] list_filter = ['user', 'course', 'Comment', 'add_time'] class UserFavoriteAdmin(object): list_display = ['user', 'fav_id', 'fav_type', 'add_time'] search_field = ['user', 'fav_id', 'fav_type'] list_filter = ['user', 'fav_id', 'fav_type', 'add_time'] xadmin.site.register(userAsk,userAsKAdmin) xadmin.site.register(UserCourse,UserCourseAdmin) xadmin.site.register(UserMessage,UserMessageAdmin) xadmin.site.register(CourseComment,CourseCommentAdmin) xadmin.site.register(UserFavorite,UserFavoriteAdmin)
而後作章節記錄表 courser app 關聯 內容代碼以下:
__author__ = "xsy" __date__ = "2018/10/25" import xadmin from .models import Course,lesson,Video,CourseResouce class CourseAdmin(object): list_display = ['name','desc','detail','degree','learn_times','students','fav_nums','image','click_nums','add_time'] search_field = ['name','desc','detail','degree','learn_times','students','fav_nums','image','click_nums',] list_filter = ['name','desc','detail','degree','learn_times','students','fav_nums','image','click_nums','add_time'] class lessonAdmin(object): list_display = ['course','name','add_time'] search_field = ['course','name',] list_filter =['course','name','add_time'] class CourseResouceAdmin(object): list_display = ['name', 'download', 'add_time'] search_field = ['name', 'download'] list_filter = ['name', 'download', 'add_time'] class VideoAdmin(object): list_display = ['lesson', 'name', 'add_time'] search_field = ['lesson', 'name'] list_filter = ['lesson', 'name', 'add_time'] xadmin.site.register(Course,CourseAdmin) xadmin.site.register(lesson,lessonAdmin) xadmin.site.register(CourseResouce,CourseResouceAdmin) xadmin.site.register(Video,VideoAdmin)
作好表關聯,咱們在作一下全站配置 好比菜單伸縮,主題配色,頁頭頁尾
咱們把配置入在user app下
先作一個基礎配置增長主題
先導入xadmin 的 views
import xadmin
加入配置:
class BaseSetting(object):
endble_themes = True
use_booswatch = True
而後在最後註冊界面
xadmin.site.register(views.BaseAdminView,BaseSetting),
到如今能夠有主題功能了,能夠新加主題。。
完整的網頁界面配好在一塊兒參考,如今接頁頭頁尾
增長配置
class GlobalSettings(object):
site_title = "這是一個面頁面的管理"
site_footer = "這是頁尾顯示的"
menu_sytle = "accordion" // 菜單伸縮
後面註冊界面
xadmin.site.register(views.CommAdminView,GlobalSettings)
在apps的名稱是在每一個app下面有個apps的文件,名字是在在那兒配置的,能夠改別名,後臺才能顯示爲中文
這些英文是apps的原名,能夠在apps 下面的apps 文件下修改
只須要在下面加一個別名的方法,好比:
class operationConfig(AppConfig)
name = "operation"
verbose_name = "用戶操做'
還得在在當前的 __init__.py 中
寫入:
default_app_config = "operation.apps.OperationConfig'
在舉一個例 咱們來改user.py 這個app
先在當前的apps.py 中找到 加入verbose_name = 「」 示例代碼以下:
class UsersConfig(AppConfig):
name = 'user'
verbose_name = "用戶"
打開 ————init__.py 增長
default_app_config = "user.apps.UserConfig"
其它幾個apps 用這個方法一致搞定,,重啓系統搞定,
到此一個網站 從數庫設計到後臺所有寫完,,關於邏輯頁面交互形式,我將在另外一章寫,這兒寫多加載慢