https://www.jianshu.com/p/9b3bfe934511 https://www.cnblogs.com/1Q84mi/p/xadmin002.html https://blog.csdn.net/killersdz/article/details/89018834
https://www.cnblogs.com/aaronthon/p/9183442.html
这是因为你在编写userprofile之后才运行的migrate,
应该在编写userprofile之前运行migratge才会生成auth_user表,
如果你直接写了userprofile的model然后运行migrate,
就会直接不生成auth_user,
你根据课程的节奏来就不会这样了,
不要随意跳过章节
settings.py
from apps import users #users是我建立的一个app sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) sys.path.insert(1, os.path.join(BASE_DIR, 'extra_apps')) INSTALLED_APPS = [ 'xadmin', 'crispy_forms', 'users', ] #扩展usermodel AUTH_USER_MODEL = 'users.UserProfile' #mysql DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'jpdata', # 数据库 'USER': 'root', # 用户名 'PASSWORD': '123456', # 密码 'HOST': '127.0.0.1', } } #中文 LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False #不知道 STATIC_ROOT = os.path.join(BASE_DIR, 'static')
urls.py
path('xadmin/', xadmin.site.urls),
adminx.py
# 样式开启和头脚设定 class BaseSetting(object): enable_themes = True use_bootswatch = True xadmin.site.register(views.BaseAdminView, BaseSetting)
----------------------------------------------------------------------
apps设定
1.创建一个users的app
models.py
from django.db import models from django.contrib.auth.models import AbstractUser class UserProfile(AbstractUser): jifen = models.fields.IntegerField(verbose_name=u"积分", default=0) # birday = models.DateField(verbose_name=u"生日", null=True, blank=True) # gender = models.CharField(max_length=8, choices=(("male", u"男"), ("female", u"女")), default="female") # address = models.CharField(max_length=100, default=u"") # mobile = models.CharField(max_length=11, null=True, blank=True) # image = models.ImageField(upload_to="image/%Y/%m", default=u"image/default.png", max_length=100) class Meta: verbose_name = "用户信息" verbose_name_plural = verbose_name def __str__(self): return self.username
adminx.py[新建]
import xadmin from .models import UserProfile class UserProfileAdmin(object): # def save_model list_display = ['username', 'first_name', 'last_name', 'email', 'is_staff', 'is_active', 'date_joined', 'last_login', 'jifen'] list_filter = ['username'] # data_charts= 'drivers-license-o' search_fields = ['id', 'username'] # model_icon = 'fa fa-user-o' list_editable = ['jifen'] # 先注销模块,再注册 xadmin.site.unregister(UserProfile) xadmin.site.register(UserProfile, UserProfileAdmin) # 设计左侧菜单 class GlobalSetting(object): #名称不能改 def get_site_menu(self): #名称不能改 return [ { 'title': '测试的', 'icon': 'fa fa-bar-chart-o', 'menus': ( { 'title': '测试子菜单1', #这里是你菜单的名称 'url': '/xadmin/test_view', #这里填写你将要跳转url 'icon': 'fa fa-cny' #这里是bootstrap的icon类名,要换icon只要登录bootstrap官网找到icon的对应类名换上即可 }, { 'title': '测试子菜单2', 'url': 'http://www.taobao.com', 'icon': 'fa fa-cny' } ) } ] site_title = "后台管理系统" site_footer = "管理系统@admin" menu_style = "accordion" #注册你上面填写的url from .views import TestView #从你的app的view里引入你将要写的view,你也可以另外写一个py文件,把后台的view集中在一起方便管理 xadmin.site.register_view(r'test_view/$', TestView, name='for_test') #注册GlobalSetting from xadmin.views import CommAdminView xadmin.site.register(CommAdminView, GlobalSetting)
apps.py
# -*- coding: utf-8 -*- from django.apps import AppConfig class UsersConfig(AppConfig): name = 'users' verbose_name = u'用户管理'
__init__.py
default_app_config = "users.apps.UsersConfig"
views.py
from django.shortcuts import render # Create your views here. from xadmin.views import CommAdminView class TestView(CommAdminView): def get(self, request): context = super().get_context() # 这一步是关键,必须super一下继承CommAdminView里面的context,不然侧栏没有对应数据,我在这里卡了好久 title = "测试子菜单123" # 定义面包屑变量 context["breadcrumbs"].append({'url': '/cwyadmin/', 'title': title}) # 把面包屑变量添加到context里面 context["title"] = title # 把面包屑变量添加到context里面 # 下面你可以接着写你自己的东西了,写完记得添加到context里面就可以了 #......... return render(request, 'users/../../templates/users/test.html', context) # 最后指定自定义的template模板,并返回context
大体结构
django版本2.2.1
ordering = ['-click_nums'] # 进入xadmin页面将某个字段倒序排列 readonly_fields = ['click_nums'] # 将某个字段以只读形式 exclude = ['fav_nums'] # 不显示某个字段