您好,登錄后才能下訂單哦!
?
?
?
django強(qiáng)大的功能之一,提供了admin后臺管理界面,簡單配置就可對數(shù)據(jù)庫內(nèi)容作管理;
?
mysite/mysite/settings.py
INSTALLED_APPS = [
??? # 'users.apps.UsersConfig',
??? 'blog.apps.BlogConfig',
??? 'publish.apps.PublishConfig',
??? 'bootstrap3',
??? 'books.apps.BooksConfig',
??? 'polls.apps.PollsConfig',
??? 'django.contrib.admin',
??? 'django.contrib.auth',?? #依賴1
??? 'django.contrib.contenttypes',?? #依賴2
??? 'django.contrib.sessions',?? #依賴3
? ??'django.contrib.messages',?? #依賴4
??? 'django.contrib.staticfiles',
]
TEMPLATES = [
??? {
??????? 'BACKEND': 'django.template.backends.django.DjangoTemplates',
??????? # 'BACKEND': 'django.template.backends.jinja2.Jinja2',
??????? 'DIRS': [os.path.join(BASE_DIR, 'templates')],
??????? 'APP_DIRS': True,
??????? 'OPTIONS': {
??????????? 'context_processors': [
??????????????? 'django.template.context_processors.debug',
??????????????? 'django.template.context_processors.request',
??????????????? 'django.contrib.auth.context_processors.auth',
??????????????? 'django.contrib.messages.context_processors.messages',
??????????? ],
??????? },
??? },
]
MIDDLEWARE = [
??? 'django.middleware.security.SecurityMiddleware',
??? 'django.contrib.sessions.middleware.SessionMiddleware',
??? 'django.middleware.common.CommonMiddleware',
??? 'django.middleware.csrf.CsrfViewMiddleware',
??? 'django.contrib.auth.middleware.AuthenticationMiddleware',
??? 'django.contrib.messages.middleware.MessageMiddleware',
??? 'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
?
?
mysite/mysite/urls.py
urlpatterns = [
??? # url(r'^$', index),
??? # url(r'^hello/$', hello),
??? # url(r'^polls/', include('polls.urls')),
??? # url(r'^polls/', include('polls.urls', app_name='polls')),
??? # url(r'^polls/', include('polls.urls', namespace='polls')),
??? # url(r'^books/', include('books.urls')),
??? # url(r'^sendmail/', include('sendmail.urls')),
??? # url(r'publish/', include('publish.urls')),
??? url(r'blog/', include('blog.urls')),
??? url(r'^admin/', admin.site.urls),
]
?
以上是前4步;
5、${app_dir}/admin.py,給每個模型創(chuàng)建一個ModelAdmin類,封裝模型自定義的Admin功能和選項;
6、注冊ModelAdmin;
到此,可通過綁定的url來訪問管理站點,默認(rèn)是/admin/;
?
注:
默認(rèn),is_superuser和is_staff雙重身份才能在admin中進(jìn)行管理和修改;
當(dāng)只有is_staff,能進(jìn)入后臺但不能進(jìn)行任何操作;
當(dāng)只有is_superuser,不能進(jìn)入admin;
>>> from django.contrib.auth.models import User
>>> User.objects.all()
<QuerySet []>
>>> u = User.objects.create_user(username='jowin',email='jowin@ane56.com')
>>> u.set_password('jowin')
>>> u.save()
>>> u
<User: jowin>
>>> u.is_superuser
False
>>> u.is_superuser=True
>>> u.is_staff
False
>>> u.is_staff=True
>>> u.save()
>>> u.is_active
True
>>> quit()
?
?
?
django默認(rèn)會到${app_name}下找admin.py模塊;
自動根據(jù)model的field類型設(shè)置form類型;
?
label名稱,定義model field時指定verbose_name;
exclude,排除某字段,不展示;
fields,顯示某字段;
date_hierarchy,添加日期標(biāo)簽過濾;
ordering,排序;
search_fields,搜索某列;
list_display,列表顯示更多列;
list_filter;
?
mysite/blog/admin.py
from django.contrib import admin
from .models import Blog, Author, Entry
?
# admin.site.register(Blog)?? #方式1
# admin.site.register(Author)
# admin.site.register(Entry)
?
@admin.register(Blog)?? #方式2
class BlogAdmin(admin.ModelAdmin):
??? pass
?
@admin.register(Entry)
class EntryAdmin(admin.ModelAdmin):
??? pass
?
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
??? pass
?
?
def authors_display(obj):
??? return ', '.join([ author.name for author in obj.authors.all()])
?
authors_display.short_description = 'Authors'
?
def make_pub_date_to_now(modeladmin, request, queryset):
??? queryset.update(pub_date=timezone.now())
?
make_pub_date_to_now.short_description = '批量修改發(fā)布日期為今天'
?
@admin.register(Entry)
class EntryAdmin(admin.ModelAdmin):
??? exclude = ('n_comments', 'n_pingbacks')
??? date_hierarchy = 'pub_date'
??? search_fields = ('headline', 'body_text')
??? list_display = ('headline', 'blog', 'pub_date', authors_display)
??? actions = [make_pub_date_to_now]
注:
web頁上label(form label),對應(yīng)models.py中的verbose_name;
?
注:
與代碼中authors_display.short_description = 'Authors'對應(yīng);
?
注:
Actions與代碼中actions = [make_pub_date_to_now]對應(yīng);
?
?
?
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。