(6) Django framework learning-Admin

Source: Internet
Author: User
Document directory
  • Admin Background Management Module
  •  
  •  
The Admin module uses the Django administrator module as part of Django. contrib, a standard library of django. This package also includes some other practical modules: django. contrib. authdjango. contrib. sessionsdjango. contrib. comments

To activate the admin module, follow these steps: 1. add 'django. contrib. admin '2. add 'django. contrib. auth ', 'django. contrib. contenttypes ''django. contrib. sessions 'because the admin module depends on 3. add 'django. middleware. common. commonMiddleware ', 'django. contrib. sessions. middleware. sessionMiddleware ''django. contrib. auth. middleware. authenticationMiddleware '. this is also because of dependency 4. run python manage. py syncdb, used to create a database table for the admin module. Because apps have the auth module, you are required to create a super user. If you cancel this step, you can also run python manage. py createsuperuser. If and only, you have installed the auth module. 5. in urls. set admin ing in py # Include these import statements... from django. contrib import adminadmin. autodiscover () # And include this URLpattern... urlpatterns = patterns ('',#... (R' ^ admin/', include (admin. site. urls )),#...) 6. remember to add the admin template file to your module. You can copy the built-in django template and add the admin folder to the module path. D: \ Python27 \ Lib \ site-packages \ django \ contrib \ admin \ templates \ admin \ base_site.html 7. run python manage. py runserver. Enter http: // localhost: 8000/admin/8 in the URL. the admin interface displays English by default. to display the required language, add 'django. middleware. locale. localeMiddleware and make sure it is placed behind SessionMiddleware. Refresh the admin page and manage the models module on the admin page: 1. you need to create an admin under your app. and register your model class from django. contrib import adminfrom mysite. books. models import Publisher, Author, Book admin. site. register (Publisher) admin. site. register (Author) admin. site. register (Book) 2. when you add a new file to the project folder, you need to restart the server to take effect. 3. if you want to add data in the admin interface to take effect, you need to add 'django. contrib. messages. middleware. messageMiddleware, custom admin module display 1. cancel when adding data, each field requires that the input restrictions be modified in the model class. class Author (models. model): first_name = models. charField (max_length = 30) last_name = models. charField (max_length = 40) email = models. emailField ( Blank = True) You only need to set blank = True in the field declaration. The default value is False. The difference between NULL and NULL strings is in the database operation, NULL and NULL string "are two different concepts. Some database types can accept the NULL value and the NULL string type, for example, varchar type, when are these two fields used? Over time, data inconsistency may occur. To avoid such a situation, Django sets the field blank = True by default, it is inserted into the database as an empty string. At the same time, when creating a data table, all fields use not null by default. However, when the time and date types are met, an error occurs when the Null String is used. In this case, NULL is required. You only need to add publication_date = models to the model field declaration. dateField (blank = True, null = True) Once you add the null = True statement, it is equivalent to changing the database table structure, similar to the alter table books_book modify publication_date date null in the SQL statement; however, as mentioned before, Django's python manage. py syncdb can only create new tables with tables, but cannot change the table structure. Therefore, you need to update the table structure in the database. On the editing page of the admin interface, the field name is displayed based on the variable name in the model class. The first letter is capitalized and the underline is changed to a space. For custom display, you can use the verbose_name parameter email = models. EmailField (blank = True, verbose_name = 'e-mail') to manage the display of the model on the admin page using ModelAdmin.
  • Custom chanle list Interface
Similar to the interface management class, ModelAdmin can define different display details. The usage is as follows: FromDjango. contrib ImportAdmin
FromMysite. books. models ImportPublisher, Author, Book

ClassAuthorAdmin (admin. ModelAdmin ):
List_display = ('first _ name', 'last _ name', 'email ') # display all the field information in search_fields = ('first _ name ', 'Last _ name') # Add the search bar. In the specified field, searchclass BookAdmin (admin. modelAdmin): list_display = ('title', 'her her ', 'publication _ date') list_filter = ('publication _ date ',) # The corresponding filter option date_hierarchy = 'publication _ date' will appear on the right of the page # only accept the field names of the date type ordering = ('-publication_date',) # Sort

Admin.Site. Register (Publisher)
Admin.Site. Register (Author, AuthorAdmin)
Admin.Site. Register (Book, BookAdmin)

  • Custom edit form Interface
Fields = ('title', 'her her ', 'authors', 'publication _ date ') # In this order, you can define the Editable field fields = ('title', 'author', 'her her ') on the edit interface. In this way, Django automatically sets publication_data to null, remember that if null = True is set to null, Django automatically provides the option to select multiple fields from allow-to-Skip. However, you can select multiple fields from other options, this method is more intuitive, but it can only be applied to multiple Select fields of allow-to-pair, and cannot be applied to the foreignkey field.

Filter_horizontal = ('authors ',)

Of course there are also filter_vertical... for the ForeighKey field, you can also set it to enter the field ID. Click the magnifier icon on the edge and the selection box raw_id_fields = ('her her ',) about users, groups, permission Django provides a detailed permission setting solution. Including the control permissions for each table.

Publish by note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.