1.引入xadmin

1.可以兼容django2.0的xadmin我上传到我的网盘里了:

链接: https://pan.baidu.com/s/1NZvPTeHDemXPPw-Ib-VeCQ 提取码: q2pp 

2.将xadmin拖入项目的extra_apps目录下:

3.安装依赖包:

pip install django-crispy-forms django-reversion django-formtools future httplib2 six django-import-export
pip install xlwt xlsxwriter

4.在settings.py中注册:

INSTALLED_APPS = [
    \'django.contrib.admin\',
    \'django.contrib.auth\',
    \'django.contrib.contenttypes\',
    \'django.contrib.sessions\',
    \'django.contrib.messages\',
    \'django.contrib.staticfiles\',
    \'users.apps.UsersConfig\',
    \'ad.apps.AdConfig\',
    \'trade.apps.TradeConfig\',
    \'user_operation.apps.UserOperationConfig\',
    
    \'crispy_forms\',
    \'xadmin\'
]

5.执行数据更新命令,生成xadmin默认的表:

python manage.py makemigrations
python manage.py migrate

6.在gg/urls.py中配置xadmin路由:

from django.contrib import admin
from django.urls import path
from django.views.static import serve
from gg.settings import MEDIA_ROOT

import xadmin

urlpatterns = [
    #path(\'admin/\', admin.site.urls),
    path(\'media/<path:path>\',serve,{\'document_root\':MEDIA_ROOT}),
    path(\'xadmin/\', xadmin.site.urls),
]

7.新建超级用户:

python manage.py createsuperuser
Username: root
Email address: 760680498@qq.com
Password:
Password (again):
#Superuser created successfully.

 创建完毕,启动项目,然后访问

http://127.0.0.1:8000/xadmin/

就可以登录xadmin后台了,但是此时后台还是英文的。

2.将后台语言换成中文

1.在settings中修改:

LANGUAGE_CODE = \'zh-hans\'

TIME_ZONE = \'Asia/Shanghai\'

USE_I18N = True

USE_L10N = True

USE_TZ = False

2.在每个app下的apps.py中做修改,加上verbose_name

1.在apps/users/apps.py中:

from django.apps import AppConfig


class UsersConfig(AppConfig):
    name = \'users\'
    verbose_name = \'用户\'

2.在apps/ad/apps.py中:

from django.apps import AppConfig


class AdConfig(AppConfig):
    name = \'ad\'
    verbose_name = \'广告和店铺\'

3.在apps/trade/apps.py中:

from django.apps import AppConfig


class TradeConfig(AppConfig):
    name = \'trade\'
    verbose_name = \'交易\'

4.在apps/user_operation/apps.py中:

from django.apps import AppConfig


class UserOperationConfig(AppConfig):
    name = \'user_operation\'
    verbose_name = \'用户操作\'

3.在每个app下新建adminx.py

1.在apps/users目录下新建adminx.py:

import xadmin
from xadmin import views
from .models import UserProfile,Area


class BaseSetting(object):
    enable_themes = True
    use_bootswatch = True


class GlobalSettings(object):
    site_title = "今日同城小广告"
    site_footer = "落忆科技网络有限责任公司"
    # menu_style = "accordion"


class UserProfileAdmin(object):
    list_display = ["username","is_staff","nickName","gender" ,"city", "jifen","yongjin","fensi_num"]
    list_display_links=["username","nickName"]
    list_filter = ["username","nickName","avatarUrl" ,"open_id", "token","gender","city" ,"longitude","latitude","jifen","yongjin","fensi_num"]
    search_fields = ["username","nickName","gender" ,"city", "jifen","yongjin","fensi_num"]


class AreaAdmin(object):
    list_display = ["num", "name", "category_type", "parent_category", "add_time"]
    list_filter = ["num", "name", "category_type", "parent_category", "add_time"]
    search_fields = ["num", "name", "category_type"]


# 先注销模块,再注册
xadmin.site.unregister(UserProfile)
xadmin.site.register(UserProfile, UserProfileAdmin)

xadmin.site.register(Area, AreaAdmin)
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)

 

注意:在UserProfileAdmin中不能加时间字段,不然会报错。

2.在apps/ad目录下新建adminx.py:

import xadmin
from .models import Category,Good,Prize,Images


class CategoryAdmin(object):
    list_display = ["name", "category_type", "parent_category", "is_tab", "add_time"]
    list_filter = ["name", "category_type", "parent_category", "is_tab", "add_time"]
    search_fields = ["name", "category_type", "parent_category", "is_tab"]


class GoodAdmin(object):
    list_display = ["stage","type","title", "desc", "address", "mobile", "pv","category","area","user","image_url","add_time"]
    list_filter = ["stage","title", "desc", "address", "mobile", "pv","category","area","user","type","add_time"]
    search_fields = ["stage","title", "desc", "address", "mobile", "pv","category","area","user","type"]


class PrizeAdmin(object):
    list_display = ["title", "get_num", "url","price", "area","all_num", "desc","usage","end_time","image_url","add_time"]
    list_filter = ["title", "get_num", "url","price", "area","all_num", "desc","usage","end_time","add_time"]
    search_fields = ["title", "get_num", "price", "all_num", "desc","usage"]


class ImagesAdmin(object):
    list_display = ["is_banner", "good", "image_url", "add_time"]
    list_filter = ["is_banner", "good","add_time"]
    search_fields = ["is_banner", "good"]


xadmin.site.register(Category, CategoryAdmin)
xadmin.site.register(Good, GoodAdmin)
xadmin.site.register(Prize, PrizeAdmin)
xadmin.site.register(Images, ImagesAdmin)

3.在apps/trade目录下新建adminx.py:

import xadmin
from .models import Price,Order,ShoppingCar


class PriceAdmin(object):
    list_display = ["top_type", "price", "fanwei", "days", "add_time"]
    list_filter = ["top_type", "price", "fanwei", "days", "add_time"]
    search_fields = ["top_type", "price", "fanwei", "days"]


class OrderAdmin(object):
    list_display = ["user", "good","order_sn", "pay_status", "trade_no", "order_mount","pay_time","add_time"]
    list_filter = ["user", "good","order_sn", "pay_status", "trade_no", "order_mount","pay_time","add_time"]
    search_fields = ["order_sn", "pay_status", "trade_no", "order_mount"]


class ShoppingCarAdmin(object):
    list_display = ["user", "order", "top_good", "goods_num", "price","add_time"]
    list_filter = ["user", "order", "top_good", "goods_num", "price","add_time"]
    search_fields = ["user", "order", "top_good", "goods_num", "price"]


xadmin.site.register(Price, PriceAdmin)
xadmin.site.register(Order, OrderAdmin)
xadmin.site.register(ShoppingCar, ShoppingCarAdmin)

 

4.在apps/user_operation目录下新建adminx.py:

import xadmin
from .models import Message,GetPrize,UserAdress,Fans,MakeMoney,GetCash


class MessageAdmin(object):
    list_display = ["user", "content", "is_read", "add_time"]
    list_filter = ["user", "content", "is_read", "add_time"]
    search_fields = ["user", "content", "is_read"]


class GetPrizeAdmin(object):
    list_display = ["user", "prize", "status", "add_time"]
    list_filter = ["user", "prize", "status", "add_time"]
    search_fields = ["user", "prize", "status"]


class UserAdressAdmin(object):
    list_display = ["user", "address", "phone", "name","add_time"]
    list_filter = ["user", "address", "phone", "name","add_time"]
    search_fields = ["user", "address", "phone", "name"]


class FansAdmin(object):
    list_display = ["user", "fans", "add_time"]
    list_filter = ["user", "fans", "add_time"]
    search_fields = ["user", "fans"]


class MakeMoneyAdmin(object):
    list_display = ["user", "fans","money","add_time"]
    list_filter = ["user", "fans","money","add_time"]
    search_fields = ["user", "fans","money"]


class GetCashAdmin(object):
    list_display = ["user","money","add_time"]
    list_filter = ["user","money","add_time"]
    search_fields = ["user","money"]


xadmin.site.register(Message, MessageAdmin)
xadmin.site.register(GetPrize, GetPrizeAdmin)
xadmin.site.register(UserAdress, UserAdressAdmin)
xadmin.site.register(Fans, FansAdmin)
xadmin.site.register(MakeMoney, MakeMoneyAdmin)
xadmin.site.register(GetCash, GetCashAdmin)

 

版权声明:本文为xuepangzi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xuepangzi/p/10798913.html