Django框架后台管理
1.创建后台管理员账户
首先,通过命令行创建一个后台管理项目——MyAdminSite
PS C:\Users\ls> cd E:\Python\
PS E:\Python> django-admin.exe startproject MyAdminSite
然后,在命令行进入该目录,并执行下面的指令启动项目
PS E:\Python> cd .\MyAdminSite\
PS E:\Python\MyAdminSite> python.exe .\manage.py runserver
下一步,通过浏览器访问默认地址(http://localhost:8000/);
接下来,继续访问后台管理模块(localhost:8000/admin/);
需要用户输入后台管理的用户名和密码才能进入模块内部界面,但是默认情况Django框架后台管理是没有配置用户名和密码的。此时,需要在命令行中通过如下指令创建管理员超级账户
PS E:\Python\MyAdminSite> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
PS E:\Python\MyAdminSite> python.exe .\manage.py createsuperuser
Username (leave blank to use 'ls'): ls
Email address: ls@hotmail.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
PS E:\Python\MyAdminSite>
2.登录后台模块
通过刚刚创建的管理员账户登录进入后台管理模块界面
管理员账户的用户名和密码验证成功后,进入的后台管理模块界面包含了管理默认的数据表功能,数据表中包含一个user项,里面包含了后台模块的账户列表。
尝试单击一下该Users链接,页面会跳转到可编辑的状态。
3.管理自定义模型
首先,在项目中新建一个app应用userinfo
PS E:\Python\MyAdminSite> django-admin.exe startapp userinfo
在该userinfo应用下创建一个模型Person,仅包括简单的姓名(name)和年龄(age)两个字段
文件路径【MyAdminSite/userinfo/models.py】
from django.db import models
# Create your models here.
class Person(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
def __str__(self):
return self.name
文件路径【MyAdminSite/userinfo/admin.py】
from django.contrib import admin
from .models import Person
# Register your models here.
admin.site.register(Person)
文件路径【MyAdminSite/MyAdminSite/settings.py】
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'userinfo.apps.UserinfoConfig',
]
后台管理界面中添加了刚刚创建了模型Person
为了更好地演示效果,可以通过python交互界面在模型中添加一些用户数据
PS E:\Python\MyAdminSite> python manage.py makemigrations
Migrations for 'userinfo':
userinfo\migrations\0001_initial.py
+ Create model Person
PS E:\Python\MyAdminSite> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, userinfo
Running migrations:
Applying userinfo.0001_initial... OK
PS E:\Python\MyAdminSite> python .\manage.py shell
Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb 4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from userinfo.models import Person
>>> Person.objects.create(name='cici',age=7)
<Person: cici>
>>>exit()
now exiting InteractiveConsole...
PS E:\Python\MyAdminSite>
4.管理复杂模型
文件路径【MyAdminSite/userinfo/models.py】
from django.db import models
# Create your models here.
# Model Dep(Department)
class Dep(models.Model):
name = models.CharField(max_length=16)
def __str__(self):
return self.name
# Model Person
class Person(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField(default=0)
dep = models.ForeignKey(Dep, on_delete=models.CASCADE,)
def __str__(self):
return self.name
文件路径【MyAdminSite/userinfo/admin.py】
from django.contrib import admin
from .models import Dep, Person
# Register your models here.
admin.site.register([Dep, Person])
刷新浏览器,添加了刚刚创建地模型Dep