Django_book sixth chapter

Six, site view management... 1

6.1 activate the management interface... 1

6.1.1 management tools... 1

6.1.2 change management interface languages... 2

6.1.3 added models to Admin management in... 3

The working principle of Admin 4 6.1.4...

Use the skills... 6.2 administration interface 4

6.2.1 set the field... 4

6.2.2 custom field label... 5

6.3 custom ModelAdmin class... 5

6.3.1 custom list... 5

6.3.2 custom editor... 8

6.4 users, user groups and permissions... 9

Six, site view management

6.1 activate the management interface

Automatic Django management tool is a part of django.contrib, is a large set of features, part of the Django code, and Django together

6.1.1 management tools

1) Modify the setting.py file

- added django.contrib.admin setting INSTALL_APPS configuration

The ---INSTALL_APPS field must contain django.contrib.auth, django.contrib.contenttypes, django.contrib.sessions, Django requires three management pack

- to ensure that MIDDLEWARE_CLASSES contains django.middleware.common.CommonMiddleware, django.contrib.sessions.middleware.SessionMiddleware, django.contrib.auth.middleware.AuthenticationMiddleware, I'm the local configuration file as follows

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',
    'debugtools',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

2

) Rebuild the management interface using additional database tables

$ python manage.py syncdb

#Prompt

After django.contrib.auth entry here, the first syncdb run time, the system will prompt you to create a super user, if I do not need to separately using $pyton manage.py createsuperuser to create a admin user account, you will not be able to log on to admin, this command must be INATALLED_APPS before use

3) The admin access configuration in url.py, the default startproject generated in url.py is the Django admin path commented, notes can be cancelled

# Uncomment the next line to enable the admin:
     url(r'^admin/', include(admin.site.urls)),

4

) Restart the server, the browser access

$ python manage.py runserver

Run the following

clip_image002[8]


5) Enter the administrator account you just created and password login management interface

clip_image004

When possible interface login and I here examples of different, by default only two modules can edit, user groups and user module


6.1.2 change management interface.

Increased django.middleware.locale.LocaleMiddleware in MIDDLEWARE_CLASSES region, and ensure the location after session, refresh your browser

'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',

clip_image006

6.1.3 added models to the management of Admin

This step is adding a module is added to the management tools, so you can make changes at the interface

1) In books, create a file admin.py is the new app (name of the custom), as follows

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,BookAdmin)

Save after restart the development server, will see the module will add their own home page appears in the management

clip_image008


2) Perfect to add their own modules

from django.contrib import admin
from mysite.books.models import Publisher, Author, Book

class BookAdmin(admin.ModelAdmin):
        title = models.CharField(max_length=100)
        authors = models.ManyToManyField(Author)
        publisher = modles.ForeignKey(Publisher)
        publication_date = models.DateField()

        def __unicode__(self):
                return self.title

admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book,BookAdmin)
The working principle of 6.1.4 Admin

1) Django from the url.py boot URLconf, then the implementation of admin.autodiscover () statement, this statement will traverse the INSTALLED_APPS block, look for the admin.py file

2) Start a file in admin.py, each call admin.site.register () module is added to the management tools

3) The application of django.contrib.auth contains its own admin.py, so users and groups can display in the admin interface, management tool is actually application, including their own module and view, URLpattern

Tips 6.2 administration interface

6.2.1 set the field optional

1) For general field

Edit the mysite/books/models.py file

class Author(models.Model):
        first_name = models.CharField(max_length=30)
        last_name = models.CharField(max_length=40)
        email = models.EmailField(
blank=True
)

When blank=True said here in the mailbox is not a required field, but optional fields, and in the management interface, email will also present the grey, and not in bold indicate optional fields,

clip_image010


2) To date, time, number field

Edit the mysite/books/models.py file

class Book(models.Model):
        title = models.CharField(max_length=100)
        authors = models.ManyToManyField(Author)
        publisher = models.ForeignKey(Publisher)
        publication_date = models.DateField(
blank=True, null=True
)

To date, time, number, if such a field will accept null values depending on your database, I MySQL here

clip_image012

#Prompt

After modification of Django will not attempt to connect to the database automatically, so for the field to update to the database

6.2.2 custom field label

Label name is not very appropriate, so you can through the verbose custom tags, we modify the e-mail alias, the first letters of Django automatic processing

class Author(models.Model):
        first_name = models.CharField(max_length=30)
        last_name = models.CharField(max_length=40)
        email = models.EmailField(blank=True, verbose_name='e-mail')

In the EmailField field, can use the following wording, the alias is passed as the first parameter, only suitable for EmailField

email = models.EmailField(‘e-mail’ ,blank=True)

clip_image014

clip_image016


6.3 custom ModelAdmin class

6.3.1 custom list

1)List information display

---Under the terms of the models in this part of the code

class Author(models.Model):
        first_name = models.CharField(max_length=30)
        last_name = models.CharField(max_length=40)
        email = models.EmailField(blank=True, verbose_name='e-mail')

        def __unicode__(self):
                return u'%s %s' % (self.first_name, self.last_name)

Defined in __unicode__ also showed first_name and last_name, and then view in browser

clip_image018

---We want to increase the output, such as the mailbox, you can sort etc.

Need to modify the books/admin.py here content

from django.contrib import admin
from mysite.books.models import Publisher, Author, Book


class AuthorAdmin(admin.ModelAdmin):
        list_display = ('first_name', 'last_name', 'email')

admin.site.register(Publisher)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Book)

#

Prompt

Define a class AuthorAdmin, inherited from the parent class django.contrib. admin.ModelAdmin, defines a list, expand the fields

Then the field in the second field in register transfer to register () function, if not defined, Django will take the default configuration

After the modification to refresh your browser

clip_image020


2) Increase the search bar

Following the admin.py appended to the following, can query first name and last name to query

class AuthorAdmin(admin.ModelAdmin):
        list_display = ('first_name', 'last_name', 'email')
        search_fields = ('first_name', 'last_name')

Refresh your browser to access

clip_image022


3) Add filter

To append the following admin.py.

from django.contrib import admin
from mysite.books.models import Publisher, Author, Book

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',)

admin.site.register(Publisher)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Book, BookAdmin)

#Prompt

The new BookAdmin class, in addition to display additional a time filter list, the filter is also applicable to other fields, when there are more than two value of the time, the filter will be displayed

Refresh your browser, find book

clip_image024


4) It can also increase the depth of the navigation bar to filter

class BookAdmin(admin.ModelAdmin):
        list_display = ('title', 'publisher', 'publication_date')
        list_filter = ('publication_date',)
        date_hierarchy = 'publication_date'

#Prompt

The date_hierarchy can only accept strings, instead of tuples

Refresh your browser

clip_image026


5) Sort

class BookAdmin(admin.ModelAdmin):
        list_display = ('title', 'publisher', 'publication_date')
        list_filter = ('publication_date',)
        date_hierarchy = 'publication_date'
        ordering = ('-publication_date',)

#Prompt

Sort by ordering behind the specified key

Refresh your browser

clip_image028

6.3.2 custom editor

1) According to the specified sequence editor

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', 'publication_date')

#Prompt

The editor will be designated in accordance with the order of arrangement

Refresh your browser

clip_image030


2) Increased js filter to the fixed field

class BookAdmin(admin.ModelAdmin):
        list_display = ('title', 'publisher', 'publication_date')
        list_filter = ('publication_date',)
        date_hierarchy = 'publication_date'
        ordering = ('-publication_date',)
        filter_horizontal = ('authors',)

#

Prompt

The field will be to add a JS editor, generally exceeds the value of many special can choose to consider this, will be a lot easier

Refresh your browser

clip_image032


4) The drop-down box to find the text box

class BookAdmin(admin.ModelAdmin):
        list_display = ('title', 'publisher', 'publication_date')
        list_filter = ('publication_date',)
        date_hierarchy = 'publication_date'
        ordering = ('-publication_date',)
        filter_horizontal = ('authors',)
        raw_id_fields = ('publisher',)

Refresh your browser

clip_image034

clip_image036

6.4 users, user groups and permissions

First, remember three Boolean marker

Activities:

Controls whether the user is activated, if this marker to a user account is closed, two users and try to login in, even if the password is correct, can not login

Member sign:

To control the user can login management interface (user non members), because the users of the system can be used to control the public pages (non management page), the logo used are public users and user management

The super user mark:

To add in the admin interface, modify and delete any items permissions, if a user account with the logo, so ownership settings will be ignored

The use of background management interface

To be continued~

Posted by Frederica at November 20, 2013 - 11:17 PM