您好,登錄后才能下訂單哦!
?
?
?
django強大的功能之一,提供了admin后臺管理界面,簡單配置就可對數據庫內容作管理;
?
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,給每個模型創建一個ModelAdmin類,封裝模型自定義的Admin功能和選項;
6、注冊ModelAdmin;
到此,可通過綁定的url來訪問管理站點,默認是/admin/;
?
注:
默認,is_superuser和is_staff雙重身份才能在admin中進行管理和修改;
當只有is_staff,能進入后臺但不能進行任何操作;
當只有is_superuser,不能進入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默認會到${app_name}下找admin.py模塊;
自動根據model的field類型設置form類型;
?
label名稱,定義model field時指定verbose_name;
exclude,排除某字段,不展示;
fields,顯示某字段;
date_hierarchy,添加日期標簽過濾;
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 = '批量修改發布日期為今天'
?
@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),對應models.py中的verbose_name;
?
注:
與代碼中authors_display.short_description = 'Authors'對應;
?
注:
Actions與代碼中actions = [make_pub_date_to_now]對應;
?
?
?
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。