Django(Chapter06 Django站点管理)
1. Django自动管理工具是django.contrib的一部分。
管理工具,从技术层面上讲,它被称作django.contrib.admin。
django.contrib中其它可用的特性,如用户鉴别系统(django.contrib.auth), 支持
匿名会话(django.contrib.sessioins)以及用户评注系统
(django.contrib.comments)。
2. 激活管理界面
$ pwd
/home/tony/djcode/mysite
$ vim settings.py
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: 'django.contrib.admin', # Uncomment the next line to enable admin documentation: 'django.contrib.admindocs', 'mysite.books', )
MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', #'django.middleware.locale.LocaleMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', )
$ python manage.py syncdb
$ python manage.py createsuperuser
from django.conf.urls.defaults import patterns, include, url # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Uncomment the admin/doc line below to enable admin documentation: (r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: (r'^admin/', include(admin.site.urls)), )
MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', # 添加了这句话,界面可以显示中文了 'django.middleware.locale.LocaleMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', )
from django.contrib import admin from mysite.books.models import Publisher, Author, Book admin.site.register(Publisher) admin.site.register(Author) admin.site.register(Book)
class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) # 建对象时,指定了email,blank=True, null=True,即这个字段是可选的 email = models.EmailField(blank=True, null=True,) def __unicode__(self): return u'%s%s'% (self.first_name, self.last_name)
class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) # 建对象时,指定了verbose_name, 那么在admin那边显示的会是 # verbose_name中指定的名字,而不再是Email。 email = models.EmailField(blank=True, null=True, \ verbose_name='e-mail')
from django.contrib import admin from mysite.books.models import Publisher, Author, Book class AuthorAdmin(admin.ModelAdmin): # 显示了list_display里面的几列 list_display=('first_name', 'last_name', 'email') class BookAdmin(admin.ModelAdmin): list_display=('title', 'publisher', 'publication_date',) admin.site.register(Publisher) admin.site.register(Author, AuthorAdmin) admin.site.register(Book,BookAdmin)
class AuthorAdmin(admin.ModelAdmin): list_display=('first_name', 'last_name', 'email') # 发现页面的头部多了一个查询 search_fields=('first_name', 'last_name')
class BookAdmin(admin.ModelAdmin): list_display=('title', 'publisher', 'publication_date',) # list_filter,发现了页面的右边多了一个publication date 过滤器 list_filter = ('publication_date',)
class BookAdmin(admin.ModelAdmin): list_display=('title', 'publisher', 'publication_date',) list_filter = ('publication_date',) # date_hierachy 发现了页面的头部多了一个时间的过滤器 date_hierarchy = 'publication_date'
class BookAdmin(admin.ModelAdmin): list_display=('title', 'publisher', 'publication_date',) list_filter = ('publication_date',) date_hierarchy = 'publication_date' # ordering, 发现头部显示的Publication date多了一个向下的箭头 # 可以点击箭头,重新排序 ordering=('-publication_date',)
class BookAdmin(admin.ModelAdmin): list_display=('title', 'publisher', 'publication_date',) list_filter = ('publication_date',) date_hierarchy = 'publication_date' ordering=('-publication_date',) # 点击Book进入编辑的时候,发现没有publication_date编辑项 fields=('title', 'authors', 'publisher',)
class BookAdmin(admin.ModelAdmin): list_display=('title', 'publisher', 'publication_date',) list_filter = ('publication_date',) date_hierarchy = 'publication_date' ordering=('-publication_date',) # fields=('title', 'authors', 'publisher',) # 点击进入编辑的时候,发现了Author当初的多选框,变成了一个精巧的 # JavaScript过滤器, 是水平排列的,可以实现全选,不选 filter_horizontal=('authors',)
class BookAdmin(admin.ModelAdmin): list_display=('title', 'publisher', 'publication_date',) list_filter = ('publication_date',) date_hierarchy = 'publication_date' ordering=('-publication_date',) # fields=('title', 'authors', 'publisher',) #filter_horizontal=('authors',) # filter_vertical和filter_horizontal功能一样,只不过显示为垂直方向 filter_vertical=('authors',)
class BookAdmin(admin.ModelAdmin): list_display=('title', 'publisher', 'publication_date',) list_filter = ('publication_date',) date_hierarchy = 'publication_date' ordering=('-publication_date',) # fields=('title', 'authors', 'publisher',) filter_horizontal=('authors',) #filter_vertical=('authors',) # raw_id_fields,当初的下拉框对于很多publisher的时候,一定不是 # 很使用的,现在变成了多选框,方便了许多。 raw_id_fields=('publisher',)