越光后端开发——ygapi(3.引入xadmin)
作者:互联网
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中修改
![复制代码](https://common.cnblogs.com/images/copycode.gif)
LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False
![复制代码](https://common.cnblogs.com/images/copycode.gif)
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界面,已经变成中文的了。
标签:xadmin,list,ygapi,add,越光,time,import,user 来源: https://www.cnblogs.com/xuepangzi/p/10505799.html