基于django快速开发一个网站(二)
使用xadmin+django构建一个后台
安装
我的系统基于django==2.0.0开发的,所以直接从源码安装
$ pip install git+git://github.com/sshwsfc/xadmin.git@django21
配置
在 settings.py 中注册相应库
INSTALLED_APPS = [ … 'xadmin', 'crispy_forms', 'reversion',]123456
添加 url 规则
from django.urls import pathimport xadmin xadmin.autodiscover()# version模块自动注册需要版本控制的 Modelfrom xadmin.plugins import xversion xversion.register_models()urlpatterns = [ # 将原先的 admin.site.urls 替换为以下 path('admin/', xadmin.site.urls),]1234567891011
登陆
╰$ python3 manage.py runserver 127.0.0.1:80011
登录 127.0.0.1:8001/admin
简单定制
注册 models
# 数据模型# users/models.pyfrom django.db import models# Create your models here.class User(models.Model): nickname = models.CharField(max_length=50, blank=True) email = models.EmailField(('email address'), blank=True, unique=True) def str(self): return self.nickname1234567891011
# users/admin.pyimport xadminfrom .models import User# Register your models here.class UserAdmin(object): # 在列表中显示 nickname email 两个字段 list_display = ['nickname', 'email']xadmin.site.register(User, UserAdmin)12345678910
修改左侧菜单栏中栏目
# users/apps.pyfrom django.apps import AppConfigclass UsersConfig(AppConfig): name = 'users' # verbose_name 为菜单栏目名字 verbose_name = '用户'12345678
# users/init.pydefault_app_config='users.apps.UsersConfig'12
修改页面 title footer header
# users/admin.pyimport xadminfrom .models import Userfrom xadmin import views# Register your models here.class UserAdmin(object): list_display = ['nickname', 'email']class GlobalSetting(object): site_title = '雨夜的博客' site_header = '雨夜的博客' site_footer = '雨夜的博客'xadmin.site.register(User, UserAdmin)xadmin.site.register(views.CommAdminView, GlobalSetting)12345678910111213141516
添加搜索、过滤器
# users/admin.pyclass UserAdmin(object): list_display = ['nickname', 'email'] # 根据 nickname 字段 搜索 search_fields = ['nickname'] list_filter = ['nickname']
赞 (0)