廣告小程序後端開發(3.xadmin的引入)

1.引入xadmin

1.能夠兼容django2.0的xadmin我上傳到個人網盤裏了:python

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

2.將xadmin拖入項目的extra_apps目錄下:git

3.安裝依賴包:django

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默認的表:session

python manage.py makemigrations
python manage.py migrate

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

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.新建超級用戶:url

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

 建立完畢,啓動項目,而後訪問spa

http://127.0.0.1:8000/xadmin/

就能夠登陸xadmin後臺了,可是此時後臺仍是英文的。code

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中:orm

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)
相關文章
相關標籤/搜索