This is the 19th day of my participation in the Genwen Challenge


Background management

Suppose we want to design a news website, and we need to write pages to display to users. Where does the news information displayed on the page come from? Find news information from the database and display it on the page. But the news on our website should be updated every day, which means the increase, delete, change, check operation of the database, so we need to write SQL statement to operate the database every day? If so, is not very tedious, so we can design a page, through the operation of this page to achieve the news database add, delete, change and check operation. So the problem comes, the boss said we need to build a new website, is it necessary to design a page to achieve the new website database add, delete, change, check operation, but such a page has a great repeatability, there is no way to let us quickly generate management database table page? Yes, thats what we are going to talk about next: Django admin. Django automatically generates admin pages based on defined model classes.

To use Django’s admin module, follow these steps:

  • Localization of management interface
  • Creating an Administrator
  • Register model classes
  • Customize the management page


1. Localization of management interface

Localization refers to the use of local habits such as language and time display. Localization here refers to the localization of China. In mainland China, simplified Chinese is used, and the time zone is Asia/Shanghai.

Open settings.py and find the language code and time zone Settings and change them to the following:

LANGUAGE_CODE = 'zh-hans'	Use The Chinese language
TIME_ZONE = 'Asia/Shanghai' Use Shanghai time, China
Copy the code


2. Create an administrator

Enter the user name, email address, password, and confirm password as prompted.

python manage.py createsuperuser
Copy the code

You will also be prompted to set the strength of your password.

Next, start the server.

python manage.py runserver
Copy the code

Open the browser, enter the following address in the address box, and press Enter.

http://127.0. 01.:8000/admin/
Copy the code

Enter the administrator account password you created to log in to the Django admin page.

Click on users to view the administrator user table, where the information for the newly created user is displayed.


3. Register model classes

After logging in to the background management, there is no model class defined in the application created by default. You need to register the model class in admin.py in your application to see it in the background management and add, delete, change, and check it.

Open the book/admin.py file and write the following code:

from django.contrib import admin
from booktest.models import BookInfo

admin.site.register(BookInfo)
Copy the code

Refresh the page in your browser to see the management of the model class BookInfo.


Djangos backstage manages operations on database tables

From the previous article, we talked about how to manipulate a database table in the shell terminal of a project. Now we’ll look at how to manipulate a database table in the Django background


The query

Click “Book Infos” to take you to the list page, which has only one column by default.


Add/modify

There is no book information at present. Click “Add” on the list page to enter the add page. Django generates a different form control based on the model class.


Click the first column of a row in the list page to go to the modify page.


delete

Modify the content as prompted, and enter the list page after the modification is successful. Click “Delete” on the modify page to delete an item.


Add more books Select the check box you want to delete on the list page to delete more books.


Click “Execute” to enter the confirm page, delete and return to the list page.


You can customize the background management page

Only the BookInfo Object is displayed on the list page, and the other properties of the object are not listed, making viewing very inconvenient. Django provides the ability to customize the admin page.

Such as what values to display on the list page.

Open the admin.py file and customize the class from admin.modeladmin.

  • The list_display attribute indicates which attributes are to be displayed
class BookInfoAdmin(admin.ModelAdmin):
    list_display = ['id', 'title', 'author', 'pub_date']
Copy the code

Note: Elements in the list_display list must be attributes in the model class, or fields in the database table corresponding to the model class.

  • Modify the registration code for the model class BookInfo as follows
admin.site.register(BookInfo, BookInfoAdmin)
Copy the code
  • Refresh the BookInfo list page and all properties are displayed


Note: The columns on the BookInfo list page become Chinese because the attribute value in the BookInfo model class is given the verbose_name parameter, and if not, the class attribute name is displayed. But found no left or English.

We simply add a Meta class to the model class to set the corresponding properties.

from django.db import models


class BookInfo(models.Model) :
    """ Book Model class """
    title = models.CharField(verbose_name=U 'Book Name', max_length=20)

    author = models.CharField(verbose_name=U 'Book author', max_length=20)

    pub_date = models.DateField(verbose_name=U 'Date of publication')

    class Meta:
        # table detail remarks
        verbose_name = 'Book information'
        
        # plural form
        verbose_name_plural = verbose_name
        
        Set the sort field
        ordering = ['id']
Copy the code

If you have set the id ordering field, the table will be ordered in ascending order without ordering id = [‘-id’].


Note: If verbose_name_plural is not specified, the model BookInfo class will have an S after the name in the browser interface.


The public,

Create a new folder X

Nature took tens of billions of years to create our real world, while programmers took hundreds of years to create a completely different virtual world. We knock out brick by brick with a keyboard and build everything with our brains. People see 1000 as authority. We defend 1024. We are not keyboard warriors, we are just extraordinary builders of ordinary world.