Django Admin 介绍
配置
在 admin.py
下重写 admin.site
里面的属性值
# 项目名称,显示在登录页和管理页面的左上角
admin.site.site_header = 'xx 项目管理系统'
# html -> head -> title | 后面的信息
admin.site.site_title = '登录系统后台'
# /admin/ 页面 html -> head -> title | 前面的信息
admin.site.index_title = '后台管理'
Django Admin
Commands
应用能通过 manage.py
注册自定义活动,如调用django
里models.py
或者views.py
的函数对数据库做些增删改查的操作,可以加入crontab
做管理,也可以用django commands
来搞。
示例:
tree test/
test/
├── __init__.py
├── admin.py
├── apps.py
├── management
│ ├── __init__.py
│ └── commands
│ ├── __init__.py
│ ├── delete_author.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
├── models.py
├── tests.py
└── views.py
3 directories, 11 files
简单写一个函数,功能是删除test_author表的一条记录:
cat test/management/commands/delete_author.py
from django.core.management.base import BaseCommand, CommandError
from test.models import Author
class Command(BaseCommand):
help = 'delete author'
def add_arguments(self, parser):
parser.add_argument('author_id', nargs='+', type=int)
def handle(self, *args, **options):
for author_id in options['author_id']:
try:
author = Author.objects.get(pk=author_id)
except Author.DoesNotExist:
raise CommandError('Author "%s" does not exist' % author_id)
author.delete()
self.stdout.write(self.style.SUCCESS('Successfully delete author "%s"' % author_id))
执行
python manage.py delete_author 2
Successfully delete author "2"
扩展: