1. 程式人生 > >越光後端開發——ygapi(3.引入xadmin)

越光後端開發——ygapi(3.引入xadmin)

wid obj pic 超級用戶 last 管理 執行 send message

1.引入xadmin

1.將xadmin文件夾放入extra_apps目錄下:

技術分享圖片

2.在每個app下新建adminx.py

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

import xadmin
from xadmin import views
from .models import WxUsers


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


class GlobalSettings(object):
    site_title 
= "越光後臺管理系統" site_footer = "yg" # menu_style = "accordion" class WxUsersAdmin(object): list_display = ["open_id", "token", "nickName", "gender","province","city","country","last_login_time","add_time"] list_filter = [ "gender","province","city","country","last_login_time","add_time
"] search_fields = ["gender","province","city","country" ] xadmin.site.register(WxUsers, WxUsersAdmin) xadmin.site.register(views.BaseAdminView, BaseSetting) xadmin.site.register(views.CommAdminView, GlobalSettings)

2.apps/wish/目錄下新建adminx.py:

import xadmin
from .models import Wish, News


class WishAdmin(object): list_display = [wxuser, content,"status","pv_num","up_num", "share_num","bb_num","end_time","add_time"] list_filter =[wxuser, content,"status","pv_num","up_num", "share_num","bb_num","end_time","add_time"] search_fields=[wxuser, content,"status","pv_num","up_num", "share_num","bb_num"] class NewsAdmin(object): list_display = [user, banner,"title","content","add_time"] list_filter =[user, banner,"title","content","add_time"] search_fields=[user, banner,"title","content"] xadmin.site.register(Wish, WishAdmin) xadmin.site.register(News, NewsAdmin)

3.apps/user_operation/目錄下新建adminx.py:

import xadmin
from .models import Message,Pick,Share,Notice


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


class PickAdmin(object):
    list_display = [user, wish,"action","add_time"]
    list_filter =[user, wish,"action","add_time"]
    search_fields=[user, wish]


class ShareAdmin(object):
    list_display = [user, wish,"add_time"]
    list_filter =[user, wish,"add_time"]
    search_fields=[user, wish]


class NoticeAdmin(object):
    list_display = [admin, content,"add_time"]
    list_filter =[admin, content,"add_time"]
    search_fields=[admin, content]


xadmin.site.register(Message, MessageAdmin)
xadmin.site.register(Pick, PickAdmin)
xadmin.site.register(Share, ShareAdmin)
xadmin.site.register(Notice, NoticeAdmin)

3.安裝和配置xadmin依賴包

1.安裝依賴包 django-crispy-forms django-reversion django-formtools future httplib2 six django-import-export

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

2.在settings中註冊

技術分享圖片

3.安裝用來操作和導出Excel文件的包 xlwt xlsxwriter

pip install xlwt xlsxwriter

4.打開終端,執行數據更新命令,生成xadmin默認的表

python manage.py makemigrations
python manage.py migrate

5.配置url,在ygapi/urls.py:

from django.contrib import admin
from django.urls import path
import xadmin

urlpatterns = [
    # path(‘admin/‘, admin.site.urls),
    path(xadmin/, xadmin.site.urls),
]

註意:真正上線之時,要把xadmin的路由設置一串密文那樣復雜,面對真正的滲透高手,隱藏好後臺管理系統的URL是唯一有意義的防範措施。

6.創建超級用戶

python manage.py createsuperuser
Username: admin
郵箱:
Password:
Password (again):

創建完畢,啟動項目,然後訪問

http://127.0.0.1:8000/xadmin/

就可以登錄xadmin後臺了,但是此時後臺還是英文的。

7.將語言設定為中文

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 = 用戶

技術分享圖片

1.apps/user_operation/apps.py:

from django.apps import AppConfig


class UserOperationConfig(AppConfig):
    name = user_operation
    verbose_name = 用戶操作

1.apps/wish/apps.py:

from django.apps import AppConfig


class WishConfig(AppConfig):
    name = wish
    verbose_name = 願望

這時再看xadmin界面,已經變成中文的了。

技術分享圖片

越光後端開發——ygapi(3.引入xadmin)