您好,登錄后才能下訂單哦!
一、前言:
在創(chuàng)建完Django項(xiàng)目后,每個(gè)app下,都會(huì)有一個(gè)urls.py文件,里邊會(huì)有如下幾行:
from django.contrib import admin
urlpatterns = [
url(r'^admin/',admin.site.urls),
]
上邊看到導(dǎo)入的admin模塊
二、主要功能:
功能1:提供數(shù)據(jù)庫(kù)管理功能
基于admin模塊,可以實(shí)現(xiàn)類似數(shù)據(jù)庫(kù)客戶端的功能,對(duì)數(shù)據(jù)進(jìn)行增刪改查
功能2:二次開發(fā)
基于該模塊的數(shù)據(jù)管理功能,可以二次定制一些實(shí)用的功能。
三、使用方法:
1、步驟1:在settings.py中,INSTALLED_APPS中,加入django.contrib.admin(默認(rèn)情況已經(jīng)有。)
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01', #自己的應(yīng)用 ]
2、步驟2:urls.py創(chuàng)建admin路由(默認(rèn)情況已經(jīng)有)
urlpatterns = [ url(r'^admin/',admin.site.urls), ...... ]
3、步驟3:在settings.py配置文件中,中間件注冊(cè)(默認(rèn)情況已經(jīng)有)
MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
4、步驟4:創(chuàng)建admin管理用戶
python3 manage.py createsuperuser [root@Centos7-33 test1]# python3 manage.pycreatesuperuser Username (leave blank to use 'root'):sunday Email address: sunday@itour.cc Password: Password (again): The password is too similar to the emailaddress. Password: Password (again): Superuser created successfully.
5、步驟5:在app 的models.py文件中創(chuàng)建表信息,即表類
from django.db import models from time import strftime class userinfo(models.Model): gender_choices = ( (0, "男"), (1, "女"), ) nid = models.AutoField(primary_key=True) name = models.CharField(max_length=30, verbose_name='用戶名') email = models.EmailField(db_index=True,verbose_name='郵箱地址') memo = models.TextField(verbose_name='備注') img = models.ImageField(upload_to=strftime('%Y%m%d'),verbose_name='圖片路徑') user_type = models.ForeignKey("UserType", null=True, blank=True) gender = models.IntegerField(choices=gender_choices,default=0) def __str__(self): return self.name class UserType(models.Model): name = models.CharField(max_length=32) def __str__(self): return self.name
6、步驟6:在app的 admin.py中注冊(cè)表
from django.contrib import admin # Register yourmodels here. from .models import userinfo,UserType admin.site.register(userinfo) admin.site.register(UserType)
7、步驟7:生成表結(jié)構(gòu)
python3 manage.py makemigrations python3 manage.py makemigrations app01 #app01為項(xiàng)目應(yīng)用名稱 python3 manage.py migrate #同步數(shù)據(jù)庫(kù)
注意:在數(shù)據(jù)庫(kù)表結(jié)構(gòu)發(fā)生變動(dòng)時(shí),再次執(zhí)行python manage.py makemigrations就會(huì)報(bào)錯(cuò),這是因?yàn)榇藭r(shí)表中可能已經(jīng)有數(shù)據(jù),貌然變動(dòng)表結(jié)構(gòu)會(huì)有影響,此時(shí)的解決方法有兩種,一種是,設(shè)置新增字段的default='xxx'某個(gè)值,但是這種通用性不是很好。另外一種是,設(shè)置該字段null=True。即允許為空即可
8、步驟8:運(yùn)行項(xiàng)目,登陸管理界面
python3 manage.py runserver 0.0.0.0:8000
訪問URL:http://IP:8000/admin
四、最終展示頁(yè)面
免責(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)容。