環境:
物理機為win7,安裝vm workstation14虛擬機軟件,虛擬機系統為centos7,python版本為3.5.2。
定義模型類:
]# 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()
配置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/')
創建數據庫test3:
]# mysql -h192.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, 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
查詢數據庫:
> 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.py
from django.contrib import admin
from .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 createsuperuser
Username (leave blank to use 'root'):
輸入root;
Email address:
輸入郵箱:[email protected]
Password:
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.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)
刷新web頁面:
可以觀察到,顯示內容有所變化。