溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

django之a(chǎn)dmin管理界面

發(fā)布時(shí)間:2020-08-11 20:33:58 來(lái)源:網(wǎng)絡(luò) 閱讀:711 作者:crystaleone 欄目:開發(fā)技術(shù)

環(huán)境:

物理機(jī)為win7,安裝vm workstation14虛擬機(jī)軟件,虛擬機(jī)系統(tǒng)為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/')

創(chuàng)建數(shù)據(jù)庫(kù)test3:

]#?mysql?-h292.168.255.70?-uroot?-p
輸入數(shù)據(jù)庫(kù)授權(quán)密碼,登錄到數(shù)據(jù)庫(kù)。

>?create?database?test3?charset=utf8;
>?use?test3;

創(chuàng)建遷移:

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

執(zhí)行遷移:

]#?python?manage.py?migrate
顯示過(guò)程:
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

查詢數(shù)據(jù)庫(kù):

>?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)

注冊(cè)模型類:有2中方法注冊(cè)

]#?vim?bookshop/admin.py
from?django.contrib?import?admin
from?.models?import?*
class?BookInfoAdmin(admin.ModelAdmin):
????list_display?=?['btitle']
#定義一個(gè)web頁(yè)面顯示的bookinfo類,添加各字段后,顯示哪些內(nèi)容,由list_display里的列表項(xiàng)定義,該列表可顯示的名稱,為該類中的屬性字段。
admin.site.register(BookInfo,?BookInfoAdmin)
#另一種注冊(cè)方式是通過(guò)裝飾器注冊(cè)
#?from?django.contrib?import?admin
#?from?.models?import?*
#?@admin.register(BookInfo)
#?class?BookInfoAdmin(admin.ModelAdmin):
#?????list_display?=?['id','btitle','bpub_date']

可顯示字段的名稱為BookInfo類中的屬性名稱:


django之a(chǎn)dmin管理界面

創(chuàng)建django管理員賬號(hào):

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

運(yùn)行django服務(wù)器

]#?python?manage.py?runserver?192.168.255.70:8000

瀏覽器訪問(wèn):192.168.255.70:8000/admin


django之a(chǎn)dmin管理界面

用戶名輸入:root

密碼輸入:root

django之a(chǎn)dmin管理界面

點(diǎn)擊Book infos:

django之a(chǎn)dmin管理界面

點(diǎn)擊右側(cè),增加book info;填寫任意內(nèi)容后,點(diǎn)擊保存:

django之a(chǎn)dmin管理界面

顯示為:

django之a(chǎn)dmin管理界面

更改顯示的字段為'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頁(yè)面:

django之a(chǎn)dmin管理界面

可以觀察到,顯示內(nèi)容有所變化。


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI