Environmental Science:
The physical machine is win7, the VM workstation 14 virtual machine software is installed, the virtual machine system is centos7, and the python version is 3.5.2.
Define model class:
]# cd /root/py3/django-test1/test5 ]# vim bookshop/models.py from django.db import models class 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()
To configure settings.py:
]# vim test5/settings.py INSTALLED_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/')
Create database test3:
]# mysql -h192.168.255.70 -uroot -p Enter the database authorization password and log in to the database. > create database test3 charset=utf8; > use test3;
Create migration:
]# python manage.py makemigrations //Display process: Migrations for 'bookshop': 0001_initial.py: - Create model BookInfo - Create model HeroInfo - Create model UserInfo
Perform migration:
]# python manage.py migrate //Display process: Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: sessions, admin, contenttypes, bookshop, auth Synchronizing 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
Query database:
> show tables; > desc bookshop_userinfo; Show: +----------+-------------+------+-----+---------+----------------+ | 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)
Registration model class: there are 2 methods to register
]# vim bookshop/admin.py from django.contrib import admin from .models import * class BookInfoAdmin(admin.ModelAdmin): list_display = ['btitle'] #Define a bookinfo class displayed on a web page. After each field is added, what content will be displayed will be defined by the list item in list display. The name that the list can display is the property field in the class. admin.site.register(BookInfo, BookInfoAdmin)
#Another way to register is through a decorator # from django.contrib import admin # from .models import * # @admin.register(BookInfo) # class BookInfoAdmin(admin.ModelAdmin): # list_display = ['id','btitle','bpub_date']
The name of the displayable field is the property name in the BookInfo class:
Create django administrator account:
]# python manage.py createsuperuser Username (leave blank to use 'root'): Enter root; Email address: Input email: root@qq.com Password: Password (again): Repeatedly input the password twice: root; Superuser created successfully.
Run django server:
]# python manage.py runserver 192.168.255.70:8000
Browser access: 192.168.255.70:8000/admin
User name input: root
Password input: root
Click Book infos:
Click the right side to add book info; after filling in any content, click Save:
It is displayed as:
Change the displayed fields to "Id", "btitle", "bpub" date ", and modify admin.py:
]# vim bookshop/admin.py from django.contrib import admin from .models import * # Register your models here. class BookInfoAdmin(admin.ModelAdmin): list_display = ['id', 'btitle', 'bpub_date'] admin.site.register(BookInfo, BookInfoAdmin)
Refresh web page:
It can be observed that the display changes.