Home  >  Article  >  Backend Development  >  Django Admin management tool

Django Admin management tool

不言
不言Original
2018-05-03 10:03:131409browse

This article mainly introduces the Django Admin management tool, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Django Admin management tool

Django The automatic management tool is part of django.contrib. You can see it in INSTALLED_APPS in the project's setting.py:

INSTALLED_APPS = (    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',
)

django.contrib is a huge feature set , which is an integral part of Django's code base.

Use management tools

Start the development server, and then access http://127.0.0.1:8000/admin/ in the browser to enter the management interface. We can create a super user through the command python manage.py createsuperuser, as shown below:

python manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address: admin@wqy.com
Password:
Password (again):
Superuser created successfully.

In order to let the admin interface manage a certain data model. We need to register the data model to admin first. For example, we created the model Student in models.py before, modify admin.py:

from django.contrib import adminfrom stu.models import Student# 1. 注册的第一种方式# admin.site.register(Student, StudentAdmin)# 第二种注册方式@admin.register(Student)class StudentAdmin(admin.ModelAdmin):

    def set_sex(self):
        if self.sex:            return '男'
        return '女'

    # 修改性别字段描述
    set_sex.short_description = '性别'
    # 展示字段
    list_display = ['id', 'name', set_sex]    # 过滤
    list_filter = ['name']    # 搜索
    search_fields = ['name']    # 分页
    list_per_page = 4

Use the command python manage .py runserver Run the program and open the URL http://127.0.0.1:8000/admin/. The interface is as follows:
Django Admin management tool

We have added some filtering statements to the above code. Click on student and see the following effect:
Django Admin management tool

The above is the detailed content of Django Admin management tool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn