Django默认为我们提供了后台管理系统admin, urls.py中配置的第一条就是访问后台管理系统admin的
urlpatterns = [ url(r'^admin/', admin.site.urls), ]
启动服务,访问127.0.0.1:8000/admin即可访问,访问后台需要验证,但是Django并没有为我们自动创建账号密码,Django提供了命令,让我们自己创建超级用户
现在我们可以用刚刚创建的超级用户登录进去(账号:admin 密码:admin123456)
进入后,发现后台管理系统是英文的,我们可以在settings.py中配置语言和时区等
# Internationalization # https://docs.djangoproject.com/en/1.9/topics/i18n/ LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False #设置为False则使用本地时间,否则会使用国际时间
在Django当中,我们可以把所有的数据表都注册进来,然后,我们就可以对这些表进行增删改查 ,打开后台会发现Django已经为我们自动注册了一个组,这个组对应的是auth_group表
本来Django也会有注册user的,因为我们重载了userprofile所以就取消了,现在我们把userprofile重新注册进来,编辑apps.users.admin.py
from django.contrib import admin from .models import UserProfile class UserProfileAdmin(admin.ModelAdmin): pass admin.site.register(UserProfile, UserProfileAdmin)
刷新后台,可以看到‘用户信息’已经注册进来了
上面演示了如何注册userprofile到admin, 要注册其他的表也是一样的,编辑各自apps下面的admin.py注册即可。下面我着重使用另一款后台管理系统xadmin,它是基于Django自带的admin开发的,但是它比admin更加强大
为了方便管理,我们在项目中新建一个python package命名为‘extra_apps’,用来存放第三方源码包
解压zip包,里面有一个目录xadmin,把xadmin复制到extra_apps中
对extrap_apps右键进行Mark Directory as Source root,使得编辑器查找路径
配置settings.py添加搜索路径
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
配置settings.py注册apps
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', 'courses', 'organization', 'operation', 'xadmin', 'crispy_forms', ]
pip安装一些依赖包
workon mxonline pip install django-crispy-forms pip install django-formtools pip install httplib2 pip install future pip install six pip install django-import-export
编辑urls.py,添加xadmin的路由
import xadmin urlpatterns = [ #url(r'^admin/', admin.site.urls), url(r'^xadmin/', xadmin.site.urls), ]
把之前userprofile在admin中的注册去掉
同步xadmin的一些表
启动服务,访问后台127.0.0.1:8000/xadmin, 账号密码还是我们之前创建的超级用户
注册app model到xadmin
users app
在users目录下新建adminx.py,xadmin会自动搜寻该文件来注册model
import xadmin from .models import UserProfile, EmailVerifyRecord, Banner class UserProfileAdmin: pass class EmailVerifyRecordAdmin: list_display = ['code', 'email', 'send_type', 'send_time'] search_fields = ['code', 'email', 'send_type'] list_filter = ['code', 'email', 'send_type', 'send_time'] class BannerAdmin: list_display = ['title', 'image', 'url', 'index', 'add_time'] search_fields = ['title', 'image', 'send_type'] list_filter = ['title', 'image', 'url', 'index', 'add_time'] # xadmin.site.register(UserProfile, UserProfileAdmin) #因为之前注册过,这里就注释了 xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin) xadmin.site.register(Banner, BannerAdmin)
courses app
在courses目录下新建adminx.py
import xadmin from .models import Course, Lesson, Video, CourseResource class CourseAdmin: list_display = ['name', 'desc', 'degree', 'learn_times', 'students', 'fav_nums', 'image', 'click_nums', 'add_time'] search_fields = ['name', 'desc', 'degree', 'learn_times', 'students', 'fav_nums', 'click_nums'] list_filter = ['name', 'desc', 'degree', 'students', 'fav_nums', 'image', 'click_nums', 'add_time'] class LessonAdmin: list_display = ['course', 'name', 'add_time'] search_fields = ['course', 'name'] list_filter = ['course__name', 'name', 'add_time'] #指定外键的字段name,用两个下划线 class VideoAdmin: list_display = ['lesson', 'name', 'add_time'] search_fields = ['lesson', 'name'] list_filter = ['lesson__name', 'name', 'add_time'] class CourseResourceAdmin: list_display = ['course', 'name', 'download', 'add_time'] search_fields = ['course', 'name', 'download'] list_filter = ['course__name', 'name', 'add_time'] xadmin.site.register(Course, CourseAdmin) xadmin.site.register(Lesson, LessonAdmin) xadmin.site.register(Video, VideoAdmin) xadmin.site.register(CourseResource, CourseResourceAdmin)
organization app
在organization目录下新建adminx.py
import xadmin from .models import CityDict, CourseOrg, Teacher class CityDictAdmin: list_display = ['name', 'desc', 'add_time'] search_fields = ['name', 'desc'] list_filter = ['name', 'desc', 'add_time'] class CourseOrgAdmin: list_display = ['name', 'desc', 'click_nums', 'fav_nums', 'image', 'address', 'city', 'add_time'] search_fields = ['name', 'desc', 'click_nums', 'fav_nums', 'address', 'city'] list_filter = ['name', 'desc', 'click_nums', 'fav_nums', 'address', 'city__name', 'add_time'] class TeacherAdmin: list_display = ['org', 'name', 'work_years', 'work_company', 'work_position', 'points', 'click_nums', 'fav_nums', 'add_time'] search_fields = ['org', 'name', 'work_years', 'work_company', 'work_position', 'points', 'click_nums', 'fav_nums'] list_filter = ['org__name', 'name', 'work_years', 'work_company', 'work_position', 'points', 'click_nums', 'fav_nums', 'add_time'] xadmin.site.register(CityDict, CityDictAdmin) xadmin.site.register(CourseOrg, CourseOrgAdmin) xadmin.site.register(Teacher, TeacherAdmin)
operation app
在operation目录下新建adminx.py
import xadmin from .models import UserAsk, CourseComments, UserFavorite, UserMessage, UserCourse class UserAskAdmin: list_display = ['name', 'mobile', 'course_name', 'add_time'] search_fields = ['name', 'mobile', 'course_name'] list_filter = ['name', 'mobile', 'course_name', 'add_time'] class CourseCommentsAdmin: list_display = ['user', 'course', 'comments', 'add_time'] search_fields = ['user', 'course', 'comments'] list_filter = ['user__nick_name', 'course__name', 'comments', 'add_time'] class UserFavoriteAdmin: list_display = ['user', 'fav_id', 'fav_type', 'add_time'] search_fields = ['user', 'fav_id', 'fav_type'] list_filter = ['user__nick_name', 'fav_id', 'fav_type', 'add_time'] class UserMessageAdmin: list_display = ['user', 'message', 'has_read', 'add_time'] search_fields = ['user', 'message', 'has_read'] list_filter = ['user', 'message', 'has_read', 'add_time'] class UserCourseAdmin: list_display = ['user', 'course', 'add_time'] search_fields = ['user', 'course', 'add_time'] list_filter = ['user__nick_name', 'course__name', 'add_time'] xadmin.site.register(UserAsk, UserAskAdmin) xadmin.site.register(CourseComments, CourseCommentsAdmin) xadmin.site.register(UserFavorite, UserFavoriteAdmin) xadmin.site.register(UserMessage, UserMessageAdmin) xadmin.site.register(UserCourse, UserCourseAdmin)
xadmin全局配置
使用xadmin的主题功能,编辑users.adminx.py
from xadmin import views class BaseSetting: enable_themes = True use_bootswatch = True xadmin.site.register(views.BaseAdminView, BaseSetting)
修改左上角logo,及下面的版权信息,编辑users.adminx.py
class GlogbalSettings: site_title = '慕学后台管理系统' site_footer = '慕学在线网' xadmin.site.register(views.CommAdminView, GlogbalSettings)
左侧边栏很长,可以让它收起来,编辑users.adminx.py
class GlogbalSettings: site_title = '慕学后台管理系统' site_footer = '慕学在线网' menu_style = "accordion"
app显示的是英文,我们想把它显示为我们自定义的名称
这个其实很简单,在我们创建app的时候,Django会在该app下自动创建apps.py文件,这个文件就是用于配置app显示名称的
users
编辑users.apps.py
from django.apps import AppConfig class UsersConfig(AppConfig): name = 'users' verbose_name = '用户信息'
编辑users.__init__.py
default_app_config = 'users.apps.UsersConfig'
courses
编辑courses.apps.py
from django.apps import AppConfig class CoursesConfig(AppConfig): name = 'courses' verbose_name = '课程信息'
编辑courses.__init__.py
default_app_config = 'courses.apps.CoursesConfig'
organization
编辑organization.apps.py
from django.apps import AppConfig class OrganizationConfig(AppConfig): name = 'organization' verbose_name = '机构和教师管理'
编辑organization.__init__.py
default_app_config = 'organization.apps.OrganizationConfig'
operation
编辑operation.apps.py
from django.apps import AppConfig class OperationConfig(AppConfig): name = 'operation' verbose_name = '用户操作'
编辑operation.__init__.py
default_app_config = 'operation.apps.OperationConfig'