django之admin管理界面
环境:
物理机为win7,安装vm workstation14虚拟机软件,虚拟机系统为centos7,python版本为3.5.2。
定义模型类:
]# cd /root/py3/django-test1/test5]# vim bookshop/models.pyfrom django.db import modelsclass BookInfo(models.Model): btitle = models.CharField(max_length=20) bpub_date = models.DateTimeField(db_column='pub_date') bread = models.IntegerField(default=0) bcomment = models.IntegerField(null=False) isDelete = models.BooleanField(default=False)class HeroInfo(models.Model): hname = models.CharField(max_length=10) hgender = models.BooleanField(default=True) hcontent = models.CharField(max_length=1000) isDelete = models.BooleanField(default=False) book = models.ForeignKey(BookInfo)class UserInfo(models.Model): uname = models.CharField(max_length=10) upwd = models.CharField(max_length=40) isDelete = models.BooleanField()
配置settings.py:
]# vim test5/settings.pyINSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'bookshop',)ROOT_URLCONF = 'test5.urls'TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR,'templates')], ... },]DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'test3', 'USER': 'root', 'PASSWORD':'root', 'HOST':'192.168.255.70', 'PORT':'3306', }}LANGUAGE_CODE = 'zh-Hans'TIME_ZONE = 'Asia/Shanghai'STATIC_URL = '/static/'STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static')]MEDIA_ROOT = os.path.join(BASE_DIR,'static/upload/')
创建数据库test3:
]# mysql -h292.168.255.70 -uroot -p输入数据库授权密码,登录到数据库。> create database test3 charset=utf8;> use test3;
创建迁移:
]# python manage.py makemigrations显示过程:Migrations for 'bookshop': 0001_initial.py: - Create model BookInfo - Create model HeroInfo - Create model UserInfo
执行迁移:
]# python manage.py migrate显示过程:Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: sessions, admin, contenttypes, bookshop, authSynchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL...Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying bookshop.0001_initial... OK Applying sessions.0001_initial... OK
查询数据库:
> show tables;> desc bookshop_userinfo;显示:+----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || uname | varchar(10) | NO | | NULL | || upwd | varchar(40) | NO | | NULL | || isDelete | tinyint(1) | NO | | NULL | |+----------+-------------+------+-----+---------+----------------+4 rows in set (0.03 sec)
注册模型类:有2中方法注册
]# vim bookshop/admin.pyfrom django.contrib import adminfrom .models import *class BookInfoAdmin(admin.ModelAdmin): list_display = ['btitle']#定义一个web页面显示的bookinfo类,添加各字段后,显示哪些内容,由list_display里的列表项定义,该列表可显示的名称,为该类中的属性字段。admin.site.register(BookInfo, BookInfoAdmin)
#另一种注册方式是通过装饰器注册# from django.contrib import admin# from .models import *# @admin.register(BookInfo)# class BookInfoAdmin(admin.ModelAdmin):# list_display = ['id','btitle','bpub_date']
可显示字段的名称为BookInfo类中的属性名称:
创建django管理员账号:
]# python manage.py createsuperuserUsername (leave blank to use 'root'): 输入root;Email address: 输入邮箱:root@qq.comPassword: Password (again): 重复输入2次密码:root;Superuser created successfully.
运行django服务器:
]# python manage.py runserver 192.168.255.70:8000
浏览器访问:192.168.255.70:8000/admin
用户名输入:root
密码输入:root
点击Book infos:
点击右侧,增加book info;填写任意内容后,点击保存:
显示为:
更改显示的字段为'id', 'btitle', 'bpub_date',修改admin.py:
]# vim bookshop/admin.pyfrom django.contrib import adminfrom .models import *# Register your models here.class BookInfoAdmin(admin.ModelAdmin): list_display = ['id', 'btitle', 'bpub_date']admin.site.register(BookInfo, BookInfoAdmin)
刷新web页面:
可以观察到,显示内容有所变化。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。