Home > Article > Backend Development > Django Admin management tool
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 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.
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:
We have added some filtering statements to the above code. Click on student and see the following effect:
The above is the detailed content of Django Admin management tool. For more information, please follow other related articles on the PHP Chinese website!