下载xadmin:pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2
注册app到xadmin后台
admin.py改名为adminx.py,注册到后台
(1)users/adminx.py
# users/adminx.py __author__ = 'derek' import xadmin from xadmin import views from .models import VerifyCode class BaseSetting(object): #添加主题功能 enable_themes = True use_bootswatch = True class GlobalSettings(object): #全局配置,后台管理标题和页脚 site_title = "生鲜" site_footer = "http://www.cnblogs.com/derek1184405959/" #菜单收缩 menu_style = "accordion" class VerifyCodeAdmin(object): list_display = ['code', 'mobile', "add_time"] xadmin.site.register(VerifyCode, VerifyCodeAdmin) xadmin.site.register(views.BaseAdminView, BaseSetting) xadmin.site.register(views.CommAdminView, GlobalSettings)
修改app名字为中文
# users/apps.py from django.apps import AppConfig class UsersConfig(AppConfig): name = 'users' #app名字后台显示中文 verbose_name = "用户管理"
还需要__init__.py中修改默认配置才生效
# users/__init__.py
default_app_config = 'users.apps.UsersConfig'
(2)goods/adminx.py
# goods/adminx.py import xadmin from .models import Goods, GoodsCategory, GoodsImage, GoodsCategoryBrand, Banner, HotSearchWords from .models import IndexAd class GoodsAdmin(object): #显示的列 list_display = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price", "shop_price", "goods_brief", "goods_desc", "is_new", "is_hot", "add_time"] #可以搜索的字段 search_fields = ['name', ] #列表页可以直接编辑的 list_editable = ["is_hot", ] #过滤器 list_filter = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price", "shop_price", "is_new", "is_hot", "add_time", "category__name"] #富文本编辑器 style_fields = {"goods_desc": "ueditor"} #在添加商品的时候可以添加商品图片 class GoodsImagesInline(object): model = GoodsImage exclude = ["add_time"] extra = 1 style = 'tab' inlines = [GoodsImagesInline] class GoodsCategoryAdmin(object): list_display = ["name", "category_type", "parent_category", "add_time"] list_filter = ["category_type", "parent_category", "name"] search_fields = ['name', ] class GoodsBrandAdmin(object): list_display = ["category", "image", "name", "desc"] def get_context(self): context = super(GoodsBrandAdmin, self).get_context() if 'form' in context: context['form'].fields['category'].queryset = GoodsCategory.objects.filter(category_type=1) return context class BannerGoodsAdmin(object): list_display = ["goods", "image", "index"] class HotSearchAdmin(object): list_display = ["keywords", "index", "add_time"] class IndexAdAdmin(object): list_display = ["category", "goods"] xadmin.site.register(Goods, GoodsAdmin) xadmin.site.register(GoodsCategory, GoodsCategoryAdmin) xadmin.site.register(Banner, BannerGoodsAdmin) xadmin.site.register(GoodsCategoryBrand, GoodsBrandAdmin) xadmin.site.register(HotSearchWords, HotSearchAdmin) xadmin.site.register(IndexAd, IndexAdAdmin)
# goods/apps.py
from django.apps import AppConfig
class GoodsConfig(AppConfig):
name = 'goods'
verbose_name = '商品管理'
# good/__init__.py default_app_config = 'goods.apps.GoodsC
图片上传+图片展示(路由拼接)
1. models 字段
ex_file = models.FileField(upload_to='%m/%d') # 指定上传上来的文件的存放目录
2. adminx,py 配置图片展示
class ExpertAdmin(object):
exclude = ("created_date", "modified_date")
list_display = ("image_data", "ex_name", "ex_technology", "ex_phone", "ex_email")
readonly_fields = ('image_data',) # 必须加这行 否则访问编辑页面会报错
def image_data(self, obj):
return mark_safe('<img src="%s%s" width="60px"/>' % (settings.WEB_ROOT, obj.ex_file.url))
image_data.short_description = u'头像'
### 配置搜索
search_fields = ("=ex_name", "=ex_city")
### 筛选
list_filter = ("ex_sex", "ex_city")
xadmin.site.register(Expert, ExpertAdmin)
3. settings.py 配置图片保存位置
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # 这里定义文件的路径,优先级高
WEB_ROOT = 'http://127.0.0.1:8000'
4. 路由拼接 url.py
import xadmin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', xadmin.site.urls),
]+static(settings.MEDIA_URL,document_root =settings.MEDIA_ROOT)