我的商城开发(第四天)—xadmin添加

1、xadmin添加富文本插件

1.1 xadmin/plugins文件夹下新建文件ueditor.py

import xadmin
from xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminView
from DjangoUeditor.models import UEditorField
from DjangoUeditor.widgets import UEditorWidget
from django.conf import settings


class XadminUEditorWidget(UEditorWidget):
    def __init__(self, **kwargs):
        self.ueditor_options = kwargs
        self.Media.js = None
        super(XadminUEditorWidget, self).__init__(kwargs)


class UeditorPlugin(BaseAdminPlugin):
    def get_field_style(self, attrs, db_field, style, **kwargs):
        if style == 'ueditor':
            if isinstance(db_field, UEditorField):
                widget = db_field.formfield().widget
                param = {}
                param.update(widget.ueditor_settings)
                param.update(widget.attrs)
                return {'widget': XadminUEditorWidget(**param)}
        return attrs

    def block_extrahead(self, context, nodes):
        js = '<script type="text/javascript" src="%s"></script>' % (
            settings.STATIC_URL + "ueditor/ueditor.config.js")  # 自己的静态目录
        js += '<script type="text/javascript" src="%s"></script>' % (
            settings.STATIC_URL + "ueditor/ueditor.all.js")  # 自己的静态目录
        nodes.append(js)


xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)
xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)

1.2 把插件添加到/plugins/init.py中

PLUGINS = (
    'ueditor',
)

1.3 url配置:project/urls.py

from django.conf.urls import url, include
import xadmin

urlpatterns = [
    # path('admin/', admin.site.urls),

    url(r'^xadmin/', xadmin.site.urls),
    url(r'^ueditor/', include('DjangoUeditor.urls')),
]

1.4 解决问题

在运行xadmin过程中,点击添加组件报错: render() got an unexpected keyword argument 'renderer'

检查发现依赖关系都是正确的,那就只能是render要求的参数错了: *arg,kwargs替换为kwargs xadmin/plugins/quickform.py

xadmin/plugins/boundfield.py

2、注册APP到xadmin后台

2.1 修改users

2.1.1 users/adminx.py

import xadmin
from .models import VerifyCode, UserAddress, UserProfile
from xadmin import views


class BaseSetting(object):
    """
    添加主题功能
    """
    enable_themes = True
    use_bootswatch = True


class GlobalSettings(object):
    """
    全局配置,后台管理标题和页脚
    """
    site_title = 'Hi购了么后台运营管理平台'
    site_footer = 'http://www.higoleme.com'
    # 菜单收缩
    menu_style = 'accordion'


class VerifyCodeAdmin(object):
    list_display = ['code', 'mobile', 'add_time']


class UserAddressAdmin(object):
    list_display = ['user', 'province', 'city', 'district', 'address', 'signer_name', 'signer_mobile']


xadmin.site.register(VerifyCode, VerifyCodeAdmin)
xadmin.site.register(UserAddress, UserAddressAdmin)
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)
ngs)

2.1.2 修改APP名字为中文users/apps.py 和users/init.py中配置

# users.py
from django.apps import AppConfig


class UsersConfig(AppConfig):
    name = 'users'
    # app名字后台显示中文
    verbose_name = '用户管理'


# __init__.py
default_app_config = 'users.apps.UsersConfig'

2.2 修改goods

2.2.1 goods/adminx.py

import xadmin

from .models import GoodsCategory, Goods, GoodsImages, HotSearchWords,Banner


class GoodsCategoryAdmin(object):
    list_display = ['name', 'code', 'desc', 'category_type', 'parent_category', 'is_tab', 'discount', 'add_time']
    list_filter = ['category_type', 'parent_category', 'name']
    search_fields = ['name', ]


class GoodsAdmin(object):
    # 显示的列
    list_display = ['name', 'category', 'goods_sn', 'stocks_num', 'cost_price', 'partner_price',
                    'market_price', 'goods_intro', 'goods_desc', 'goods_show_image', 'good_front_images',
                    'is_shipfee', 'is_new', 'is_hot', 'is_exchange', 'is_grounding', 'discount']
    # 可以搜索的字段
    search_fields = ['name', ]
    # 列表页可以直接编辑的
    list_editable = ['is_hot', ]
    # 过滤器
    list_filter = ['name', 'click_num', 'sold_num', 'fav_num', 'stocks_num', 'cost_price', 'partner_price',
                   'market_price', 'is_shipfee', 'is_new', 'is_hot', 'is_exchange', 'is_grounding', 'discount',
                   'add_time']
    # 富文本编辑器
    style_fields = {'goods_desc': 'ueditor'}

    # 在添加商品的时候可以添加商品图片
    class GoodsImagesInline(object):
        model = GoodsImages
        exclude = ["add_time"]
        extra = 1
        style = 'tab'

    inlines = [GoodsImagesInline]


class BannerGoodsAdmin(object):
    list_display = ["goods", "image", "index"]


class HotSearchWordsAdmin(object):
    list_display = ["keywords", "index", "add_time"]


xadmin.site.register(GoodsCategory, GoodsCategoryAdmin)
xadmin.site.register(Goods, GoodsAdmin)
xadmin.site.register(Banner, BannerGoodsAdmin)
xadmin.site.register(HotSearchWords, HotSearchWordsAdmin)

2.2.2 修改APP名字为中文goods/apps.py 和goods/init.py中配置

# goods/apps.py
from django.apps import AppConfig


class GoodsConfig(AppConfig):
    name = 'goods'
    # app名字后台显示中文
    verbose_name = '商品管理'

# goods/__init__.py
default_app_config = 'goods.apps.GoodsConfig'

2.3 修改trade

2.3.1 trade/adminx.py

import xadmin
from .models import ShoppongCart, OrderInfo, OrderGoods, OrderStatus, ExpressType


class ShoppongCartAdmin(object):
    list_display = ['user', 'goods', 'nums']


class OrderStatusAdmin(object):
    list_display = ['order_status', 'add_time']


class ExpressTypeAdmin(object):
    list_display = ['express_type', 'add_time']


class OrderInfoAdmin(object):
    list_display = ['user', 'order_sn', 'nonce_str', 'trade_no', 'order_status', 'pay_type', 'post_script',
                    'order_count', 'ship_type', 'ship_count', 'pay_time', 'useraddress', 'expresstype', 'add_time']

    class OrderGoodsInline(object):
        model = OrderGoods
        exclude = ['add_time', ]
        extra = 1
        style = 'tab'

    inlines = [OrderGoodsInline, ]


xadmin.site.register(ShoppongCart, ShoppongCartAdmin)
xadmin.site.register(OrderInfo, OrderInfoAdmin)
xadmin.site.register(OrderStatus, OrderStatusAdmin)
xadmin.site.register(ExpressType, ExpressTypeAdmin)

2.3.2 修改APP名字为中文trade/apps.py 和trade/init.py中配置

# trade/apps.py

from django.apps import AppConfig


class TradeConfig(AppConfig):
    name = 'trade'
    # app名字后台显示中文
    verbose_name = '交易管理'

# trade/__init__.py  

default_app_config = 'trade.apps.TradeConfig'

2.4 修改user_operation

2.4.1 user_operation/adminx.py

import xadmin
from .models import UserFav, UserSaleCounseling


class UserFavAdmin(object):
    list_display = ['user', 'goods', 'add_time']


class UserSaleCounselingAdmin(object):
    list_display = ['user', 'salecounseling_type', 'content', 'add_time']


xadmin.site.register(UserFav, UserFavAdmin)
xadmin.site.register(UserSaleCounseling, UserSaleCounselingAdmin)

2.4.2 修改APP名字为中文user_operation/apps.py 和user_operation/init.py中配置

# user_operation/apps.py
from django.apps import AppConfig


class UserOperationConfig(AppConfig):
    name = 'user_operation'
    # app名字后台显示中文
    verbose_name = '操作管理'

# user_operation/__init__.py
default_app_config = 'user_operation.apps.UserOperationConfig'

3、当前目录结构


评论(0 ) 点赞(18)


暂未登录,请登录之后发表评论。 QQ