django之admin管理界面

環境:python

物理機爲win7,安裝vm workstation14虛擬機軟件,虛擬機系統爲centos7,python版本爲3.5.2。mysql


定義模型類:web

]# 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:sql

]# 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;

建立遷移:django

]# python manage.py makemigrations
顯示過程:
Migrations for 'bookshop':
  0001_initial.py:
    - Create model BookInfo
    - Create model HeroInfo
    - Create model UserInfo

執行遷移:vim

]# 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

查詢數據庫:centos

> 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類中的屬性名稱:bash


QQ截圖20190125001044.png

建立django管理員帳號:

]# python manage.py createsuperuser
Username (leave blank to use 'root'): 
輸入root;
Email address: 
輸入郵箱:root@qq.com
Password: 
Password (again): 
重複輸入2次密碼:root;
Superuser created successfully.

運行django服務器:

]# python manage.py runserver 192.168.255.70:8000

瀏覽器訪問:192.168.255.70:8000/admin


32609.png

用戶名輸入:root

密碼輸入:root

4232644.png

點擊Book infos:

4235708.png

點擊右側,增長book info;填寫任意內容後,點擊保存:

35918.png

顯示爲:

QQ截圖20190125001201.png

更改顯示的字段爲'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頁面:

QQ截圖20190125001518.png

能夠觀察到,顯示內容有所變化。