三、xadmin後臺管理 3.1.xadmin的安裝 django2.0的安裝(源碼安裝方式): 把zip文件放到pip目錄下,運行下麵命令安裝: 是文件README.rst 出現了 Unicode 解碼錯誤,這個文件是沒有什麼用處的,可以新建一個同名的空白文件替換掉 首先下載zip源碼包:gith ...
三、xadmin後臺管理
3.1.xadmin的安裝
django2.0的安裝(源碼安裝方式):
https://github.com/sshwsfc/xadmin/tree/django2
把zip文件放到pip目錄下,運行下麵命令安裝:
pip install xadmin-django2
是文件README.rst 出現了 Unicode 解碼錯誤,這個文件是沒有什麼用處的,可以新建一個同名的空白文件替換掉 首先下載zip源碼包:github.com/sshwsfc/xadmin 解壓後,打開README.rst文件,清空裡面的內容,然後保存。 再壓縮成zip,放到pip目錄下:C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Lib\site-packages\pip 此時打開cmd進行安裝:pip install xadmin-master.zip其它版本
如果上面安裝提示錯誤:
更換安裝源(使用豆瓣源)
安裝xadmin的命令如下:
pip install -i https://pypi.douban.com/simple xadmin-django2
安裝成功後,同時也安裝了很多依賴的包,後面也需要用到。
3.2.xadmin的設置
(1)新建Python Package "extra_apps",把源碼xadmin文件夾放到extra_apps文件夾下麵,此時目錄結構如下:
(2)把extra_apps右鍵mark為Source Root併在settings中加入
sys.path.insert(0,os.path.join(BASE_DIR, 'extra_apps'))
(3)因為我們用源碼的xadmin,所以要卸載之前安裝的
pip uinstall xadmin
(4)配置路由
把admin改成xadmin
# urls.py from django.urls import path import xadmin urlpatterns = [ path('xadmin/', xadmin.site.urls), ]
(5)註冊app
把下麵兩個app註冊到settings.py的INSTALLED_APPS中
'xadmin', 'crispy_forms'
(6)重新生成資料庫
python manage.py makemigrations
python manage.py migrate
(7)設置成中文
LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False
(8)創建一個管理員用戶
python manage.py createsuperuser
現在就可以運行了
python manage.py runserver
訪問後臺:http://127.0.0.1:8000/xadmin
可以看到成功進入管理界面
datetimefield報錯問題解決:
當我們點增加用戶信息,會報錯
可以看到報的是xadmin/widgets中第80行
def render(self, name, value, attrs=None): input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).split('\n') if ht != ''] # return input_html return mark_safe('<div class="datetime clearfix"><div class="input-group date bootstrap-datepicker"><span class="input-group-addon"><i class="fa fa-calendar"></i></span>%s' '<span class="input-group-btn"><button class="btn btn-default" type="button">%s</button></span></div>' '<div class="input-group time bootstrap-clockpicker"><span class="input-group-addon"><i class="fa fa-clock-o">' '</i></span>%s<span class="input-group-btn"><button class="btn btn-default" type="button">%s</button></span></div></div>' % (input_html[0], _(u'Today'), input_html[1], _(u'Now')))
上面貼出來的最後一行代碼就是widgets.py的第80行代碼。
可以看出這句代碼是希望用“\n”把input_html里的兩個標簽拆開,但兩個標簽之間沒有換行,所以沒能拆分,導致報錯。
input_html[1]就是報錯的代碼,因為input_html里只有一個元素。
解決辦法:
既然“\n”不能拆分標簽,那麼就換一種拆分方式,使用“/><”拆分。
原代碼:
input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).split('\n') if ht != '']
修改後代碼:
input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).split('/><') if ht != ''] input_html[0] = input_html[0] + "/>" input_html[1] = "<" + input_html[1]
再運行就正常了
3.3.users app的models註冊
(1)在users下麵創建adminx.py,代碼如下:
# users/adminx.py import xadmin from .models import EmailVerifyRecord #xadmin中這裡是繼承object,不再是繼承admin class EmailVerifyRecordAdmin(object): pass xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin)
(2)完善功能,增加顯示欄位,搜索和過濾
修改users/adminx.py,代碼如下:
# users/adminx.py import xadmin from .models import EmailVerifyRecord #xadmin中這裡是繼承object,不再是繼承admin class EmailVerifyRecordAdmin(object): # 顯示的列 list_display = ['code', 'email', 'send_type', 'send_time'] # 搜索的欄位,不要添加時間搜索 search_fields = ['code', 'email', 'send_type'] # 過濾 list_filter = ['code', 'email', 'send_type', 'send_time'] xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin)
刷新後的界面:
users中Banner也註冊進去
class BannerAdmin(object): list_display = ['title', 'image', 'url','index', 'add_time'] search_fields = ['title', 'image', 'url','index'] list_filter = ['title', 'image', 'url','index', 'add_time'] xadmin.site.register(Banner,BannerAdmin)
1.4.剩餘app model註冊
(1)course
代碼如下: 註意外鍵
# course/adminx.py import xadmin from .models import Course, Lesson, Video, CourseResource
class CourseAdmin(object): '''課程''' list_display = [ 'name','desc','detail','degree','learn_times','students'] search_fields = ['name', 'desc', 'detail', 'degree', 'students'] list_filter = [ 'name','desc','detail','degree','learn_times','students'] class LessonAdmin(object): '''章節''' list_display = ['course', 'name', 'add_time'] search_fields = ['course', 'name'] #這裡course__name是根據課程名稱過濾 list_filter = ['course__name', 'name', 'add_time'] class VideoAdmin(object): '''視頻''' list_display = ['lesson', 'name', 'add_time'] search_fields = ['lesson', 'name'] list_filter = ['lesson', 'name', 'add_time'] class CourseResourceAdmin(object): '''課程資源''' list_display = ['course', 'name', 'download', 'add_time'] search_fields = ['course', 'name', 'download'] list_filter = ['course__name', 'name', 'download', 'add_time'] # 將管理器與model進行註冊關聯 xadmin.site.register(Course, CourseAdmin) xadmin.site.register(Lesson, LessonAdmin) xadmin.site.register(Video, VideoAdmin) xadmin.site.register(CourseResource, CourseResourceAdmin)
(2)organizations
代碼如下:
# organization/adminx.py 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','add_time' ] search_fields = ['name', 'desc', 'click_nums', 'fav_nums'] list_filter = ['name', 'desc', 'click_nums', 'fav_nums','city__name','address','add_time'] class TeacherAdmin(object): '''老師''' list_display = [ 'name','org', 'work_years', 'work_company','add_time'] search_fields = ['org', 'name', 'work_years', 'work_company'] list_filter = ['org__name', 'name', 'work_years', 'work_company','click_nums', 'fav_nums', 'add_time'] xadmin.site.register(CityDict, CityDictAdmin) xadmin.site.register(CourseOrg, CourseOrgAdmin) xadmin.site.register(Teacher, TeacherAdmin)
(3)operation
代碼如下:
# operation/adminx.py import xadmin from .models import UserAsk, UserCourse, UserMessage, CourseComments, UserFavorite class UserAskAdmin(object): '''用戶表單我要學習''' list_display = ['name', 'mobile', 'course_name', 'add_time'] search_fields = ['name', 'mobile', 'course_name'] list_filter = ['name', 'mobile', 'course_name', 'add_time'] # class UserCourseAdmin(object): '''用戶課程學習''' list_display = ['user', 'course', 'add_time'] search_fields = ['user', 'course'] list_filter = ['user', 'course', 'add_time'] class UserMessageAdmin(object): '''用戶消息後臺''' list_display = ['user', 'message', 'has_read', 'add_time'] search_fields = ['user', 'message', 'has_read'] list_filter = ['user', 'message', 'has_read', 'add_time'] class CourseCommentsAdmin(object): '''用戶評論後臺''' list_display = ['user', 'course', 'comments', 'add_time'] search_fields = ['user', 'course', 'comments'] list_filter = ['user', 'course', 'comments', 'add_time'] class UserFavoriteAdmin(object): '''用戶收藏後臺''' list_display = ['user', 'fav_id', 'fav_type', 'add_time'] search_fields = ['user', 'fav_id', 'fav_type'] list_filter = ['user', 'fav_id', 'fav_type', 'add_time'] # 將後臺管理器與models進行關聯註冊。 xadmin.site.register(UserAsk, UserAskAdmin) xadmin.site.register(UserCourse, UserCourseAdmin) xadmin.site.register(UserMessage, UserMessageAdmin) xadmin.site.register(CourseComments, CourseCommentsAdmin) xadmin.site.register(UserFavorite, UserFavoriteAdmin)
全部代碼:
# users/adminx.py import xadmin from .models import EmailVerifyRecord,Banner #xadmin中這裡是繼承object,不再是繼承admin class EmailVerifyRecordAdmin(object): # 顯示的列 list_display = ['code', 'email', 'send_type', 'send_time'] # 搜索的欄位 search_fields = ['code', 'email', 'send_type'] # 過濾 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'] list_filter = ['title', 'image', 'url','index', 'add_time'] xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin) xadmin.site.register(Banner,BannerAdmin)users/adminx.py
# course/adminx.py import xadmin from .models import Course, Lesson, Video, CourseResource # Course的admin管理器 class CourseAdmin(object): '''課程''' list_display = [ 'name','desc','detail','degree','learn_times','students'] search_fields = ['name', 'desc', 'detail', 'degree', 'students'] list_filter = [ 'name','desc','detail','degree','learn_times','students'] class LessonAdmin(object): '''章節''' list_display = ['course', 'name', 'add_time'] search_fields = ['course', 'name'] #這裡course__name是根據課程名稱過濾 list_filter = ['course__name', 'name', 'add_time'] class VideoAdmin(object): '''視頻''' list_display = ['lesson', 'name', 'add_time'] search_fields = ['lesson', 'name'] list_filter = ['lesson', 'name', 'add_time'] class CourseResourceAdmin(object): '''課程資源''' list_display = ['course', 'name', 'download', 'add_time'] search_fields = ['course', 'name', 'download'] list_filter = ['course__name', 'name', 'download', 'add_time'] # 將管理器與model進行註冊關聯 xadmin.site.register(Course, CourseAdmin) xadmin.site.register(Lesson, LessonAdmin) xadmin.site.register(Video, VideoAdmin) xadmin.site.register(CourseResource, CourseResourceAdmin)course/adminx.py
# organization/adminx.py 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','add_time' ] search_fields = ['name', 'desc', 'click_nums', 'fav_nums'] list_filter = ['name', 'desc', 'click_nums', 'fav_nums','city__name','address','add_time'] class TeacherAdmin(object): '''老師''' list_display = [ 'name','org', 'work_years', 'work_company','add_time'] search_fields = ['org', 'name', 'work_years', 'work_company'] list_filter = ['org__name', 'name', 'work_years', 'work_company','click_nums', 'fav_nums', 'add_time'] xadmin.site.register(CityDict, CityDictAdmin) xadmin.site.register(CourseOrg, CourseOrgAdmin) xadmin.site.register(Teacher, TeacherAdmin)organization/adminx.py
# operation/adminx.py import xadmin from .models import UserAsk, UserCourse, UserMessage, CourseComments, UserFavorite class UserAskAdmin(object): '''用戶表單我要學習''' list_display = ['name', 'mobile', 'course_name', 'add_time'] search_fields = ['name', 'mobile', 'course_name'] list_filter = ['name', 'mobile', 'course_name', 'add_time'] # class UserCourseAdmin(object): '''用戶課程學習''' list_display = ['user', 'course', 'add_time'] search_fields = ['user', 'course']