Django-admin Module

Source: Internet
Author: User

The admin module using Django is part of the Django Standard library Django.contrib. This package also includes some other useful modules: Django.contrib.authdjango.contrib.sessionsdjango.contrib.comments   Activate the admin module by: 1. In the Installed_apps settings file, add ' Django.contrib.admin ' 2. Add ' Django.contrib.auth ' again, ' django.contrib.contenttypes ' ' django.contrib.sessions ' because the admin module relies on 3. Add ' Django.middleware.common.CommonMiddleware ' to the middleware_classes attribute, ' Django.contrib.sessions.middleware.SessionMiddleware ' Django.contrib.auth.middleware.AuthenticationMiddleware ' . Also because of dependence 4. Run Python manage.py syncdb to create a new database table for modules such as admin. Because there are auth modules in the apps, you will be asked to create a super user. If you cancel this step, you can also run Python manage.py createsuperuser. When and only if, you have installed the Auth module. 5. Set Admin mappings in urls.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 Django self-contained template, and note the Admin folder in 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. AdminThe interface is displayed in English by default, if you want to display the language you want: Add ' django.middleware.locale.LocaleMiddleware ' to the middleware_classes attribute, And make sure it's lined up behind Sessionmiddleware. Re-refresh admin page   Manage models module in Admin page: 1. Need to create a admin.py file 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 in the project folder, you need to restart the server to take effect.  3. If you want the Add Data feature in your admin interface to take effect, you need to add     ' in the middleware_classes attribute Django.contrib.messages.middleware.MessageMiddleware ',  The display of the custom admin Module 1. Cancel when adding data, each field needs to enter the limit in the model class to make the corresponding modification can be: Class Author (models. Model):    first_name = models. Charfield (max_length=30)     last_name = models. Charfield (max_length=40)     email = models. Emailfield ( blank=trueYou only need to set blank=true in the field declaration, the default is false about null vs. empty strings in database operations, null and empty string "" are two different concepts, some database types can accept null values, and can accept empty string types, for example, varchar type, exactly when to use the two, over time, prone to inconsistent data, in order to avoid such a situation, Django is the default blank=true such a field, uniform as an empty string inserted into the database. At the same time, when creating a data table, all fields are unified by default with not NULL. However, when a time, date type is encountered, an empty string is an error. At this point, you need to use NULL, just add in the Model field declaration: Publication_date = models. Datefield (Blank=true, null=true) Once you add the Null=true declaration, this is equivalent to changing the database table structure, like ALTER TABLE books_book modify in SQL statements _date date null; However, as previously mentioned, Django Python manage.py syncdb can only create a new table with a table and cannot change the table structure, so you need to update the table structure in the database. In the edit page of the admin interface, the display of the field name is displayed according to the variable name in the Model class, the first letter in uppercase, and the underscore to a space. Custom display can use the verbose_name parameter email = models. Emailfield (blank=true, verbose_name= ' e-mail ') uses Modeladmin to manage the display of the model in the Admin page
    • Customizing the Chanle list interface
Modeladmin Similar interface management classes, you can define different display details. Use the following: 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 ') #添加search bar, Searchclass bookadmin (Admin) in the specified field. Modeladmin): List_display = (' title ', ' publisher ', ' publication_date ') List_filter = (' publication_date ',) #页面右边会出现相应 The filter option Date_hierarchy = ' publication_date ' #只接受日期类型的字段名 ordering = ('-publication_date ',) #排序admin. site. Register (Publisher) admin. site. Register (Author, authoradmin) admin. site. Register (book, bookadmin)
    • Customizing the Edit Form interface
Fields = (' title ', ' publisher ', ' authors ', ' publication_date ') #edit界面就按这个顺序显示 can also define an editable field (' title ', ' authors ', ' Publisher ') This way Django automatically sets Publication_data to null, remembering that null=true is set to null worth when it encounters a multi-select field type of Many-to-many, Django automatically provides the option in the However, you can choose to do multiple selections in other ways, which is more intuitive, but can only be used as a multi-select field for Many-to-many, and cannot be applied to the ForeignKey field.  Filter_horizontal = (' authors ',) of course there are filter_vertical ... For the Foreighkey field, there are also settings to enter the ID of the field, click the magnifying glass icon on the edge, and the selection box will pop up raw_id_fields = (' publisher ') about the user, group, and Permissions Django provides a very detailed permission setting scheme. Includes control permissions for each table.

Django-admin Module

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.