Django Admin 介绍

发布时间: 更新时间: 总字数:390 阅读时间:1m 作者: 分享 复制网址

Django Admin 介绍

配置

admin.py 下重写 admin.site 里面的属性值

  • admin.py
# 项目名称,显示在登录页和管理页面的左上角
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 注册自定义活动,如调用djangomodels.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"

扩展:

Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数