我的博客開發之xadmin 佈局和後臺樣式

項目源碼下載:http://download.vhosts.cnpython

 

    1、 xadmin 後臺配置註冊信息django

    1. 在apps 的blogs 和 users 兩個app中添加adminx.py文件json

 

    vim apps\users\adminx.pyvim

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'cpy'
 
import xadmin
from .models import EmailVerifyRecode, UserProfile
from xadmin import views
 
# 設置主題
class BaseSettings(object):
    enable_themes = True  # 啓用主題
    use_bootswatch = True  # 啓用多主題風格,若是主題不能多變能夠跟蹤源碼看http://bootswatch.com/api/3.json是否能訪問
     
xadmin.site.register(views.BaseAdminView, BaseSettings)  # 向xadmin註冊配置的內容,若是不註冊則配置無效
 
class GlobalSettings(object):
    site_title = u'博客後臺'  # 替換默認的Django Xadmin 標題
    site_footer = u'2017 池偏一 | 贛ICP備 17014207號'  # 替換底部@
    menu_style = u'accordion'  # 設置菜單縮放
     
xadmin.site.register(views.CommAdminView, GlobalSettings)
 
class EmailVerifyRecodeAdmin(object):
    list_display = ['code', 'email', 'sendtype', 'createtime']  # 展現列表
    search_fields = ['code', 'email', 'sendtype', 'updatetime']  # 搜索使用的字段域
    list_filter = ['code', 'updatetime', 'sendtype', 'createtime']  # 過濾使用的字段域
    model_icon = 'fa fa-envelope-o'  # 當前菜單圖標
     
class UserProfileAdmin(object):
    list_display = ['username', 'gender', 'mobile', 'first_name', 'createtime']
    search_fields = ['username', 'gender', 'mobile', 'photo', 'createtime']
    list_filter = ['username', 'gender', 'mobile', 'createtime']
    model_icon = 'fa fa-user'
     
xadmin.site.register(EmailVerifyRecode, EmailVerifyRecodeAdmin)
xadmin.site.unregister(UserProfile)
xadmin.site.register(UserProfile, UserProfileAdmin)

  vim apps\blogs\adminx.pyapi

#!/usr/bin/env python  
# -*- coding:utf-8 -*-
__author__ = 'cpy'
 
import xadmin
from .models import Catagroy, Tags, Artical, Comments
from xadmin import views
 
class CatagroyAdmin(object):
    list_display = ['name', 'parent', 'createtime']
    search_fields = ['name', 'createtime']
    list_filter = ['name', 'createtime']
    model_icon = 'fa fa-list'
     
class TagsAdmin(object):
    list_display = ['name']
    search_fields = ['name', 'updatetime']
    list_filter = ['name', 'updatetime']
    model_icon = 'fa fa-tags'
     
class ArticalAdmin(object):
    list_display = ['title', 'author', 'click_num', 'createtime']
    search_fields = ['title', 'author', 'content', 'click_num', 'createtime']
    list_filter = ['title', 'author', 'content', 'click_num', 'createtime']
    list_per_page = 10 # 設置xadmin後臺展現數據,每頁10條
    list_eitable = ['content'] # 設置可編輯字段
    ordering = ['-updatetime'] # 排序
    style_fields = {'content': 'ueditor'}  # 設置content 字段使用ueditor 字段類型,xadmin與ueditor 集成後生效
    model_icon = 'fa fa-book'
     
class CommentsAdmin(object):
    list_display = ['username', 'email', 'content', 'createtime']
    search_fields = ['username', 'email', 'content', 'createtime']
    list_filter = ['username', 'email', 'content', 'createtime']
    model_icon = 'fa fa-cog fa-spin'
     
xadmin.site.register(Catagroy, CatagroyAdmin)
xadmin.site.register(Tags, TagsAdmin)
xadmin.site.register(Artical, ArticalAdmin)
xadmin.site.register(Comments, CommentsAdmin)

  

  

  2.  將blogs 和 users 兩個app中的__ini__.py 改爲以下內容,指定app默認使用的配置文件app

   vim apps\users\__ini__.py編輯器

default_app_config = 'users.apps.UsersConfig'

  vim apps\blogs\__ini__.pycode

default_app_config = 'blogs.apps.BlogsConfig'

  上述配置完成後,基本後臺樣式和數據展現就配置完成了,可是模塊菜單顯示爲英文的表名,是由於指定的app 默認配置爲: blog

class <modelname>Config(AppConfig):
            name="<modelsname>"

  

因此若是但願模塊菜單顯示爲中文只須要在每一個app 的apps.py 中加入排序

    verbose_name = u'menu_name' 便可

 

    如:

        vim cpyblog\apps\users\apps.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.apps import AppConfig
 
class UsersConfig(AppConfig):
    name = 'users'
    verbose_name = u'用戶管理'

  vim cpyblog\apps\blogs\apps.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.apps import AppConfig
 
class BlogsConfig(AppConfig):
    name = 'blogs'
    verbose_name = u'博客管理'

  

 配置完成後,xadmin後臺將變成以下樣式,但此時ueditor編輯器並未集成到xadmin後臺中,此時看到的UEditorField字段在xadmin 中顯示爲空白無樣式狀態。

    image.png

相關文章
相關標籤/搜索