Django创建超管用户

在 Django 中创建超级用户(superuser)可以通过命令行工具 createsuperuser 完成。以下是具体步骤:

1. 确保已进行数据库迁移

在创建超级用户前,确保已执行数据库迁移:

复制代码
python manage.py migrate

2. 创建超级用户

使用 createsuperuser 命令创建超级用户:

复制代码
python manage.py createsuperuser

系统会提示输入用户名、邮箱和密码:

复制代码
Username (leave blank to use 'yourusername'): admin
Email address: admin@example.com
Password: ********
Password (again): ********
Superuser created successfully.

3. 使用环境变量自动创建(可选)

在脚本或自动化部署中,可以通过环境变量自动创建超级用户:

复制代码
echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin@example.com', 'password')" | python manage.py shell

4. 在代码中创建超级用户(可选)

也可以在代码中创建超级用户:

复制代码
from django.contrib.auth import get_user_model

User = get_user_model()
User.objects.create_superuser('admin', 'admin@example.com', 'password')

5. 验证超级用户

创建完成后,使用超级用户登录 Django Admin 后台:

总结

通过 createsuperuser 命令可以轻松创建 Django 超级用户,也可以通过脚本或代码实现自动化创建。

相关推荐
怒码ing4 分钟前
List<?>和List<Object>区别
windows·python·list
那雨倾城10 分钟前
PiscCode轨迹跟踪Mediapipe + OpenCV进阶:速度估算
图像处理·人工智能·python·opencv·计算机视觉
泉城老铁12 分钟前
Spring Boot中实现多线程分片下载
java·spring boot·后端
泉城老铁13 分钟前
Spring Boot中实现多文件打包下载
spring boot·后端·架构
泉城老铁14 分钟前
Spring Boot中实现大文件分片下载和断点续传功能
java·spring boot·后端
2501_9200470315 分钟前
bash自带的切片操作
开发语言·python·bash
偷心伊普西隆16 分钟前
Python EXCEL 小技巧:最快重新排列dataframe函数
python·excel
码事漫谈21 分钟前
C++中虚函数与构造/析构函数的深度解析
后端
百思可瑞教育22 分钟前
Spring Boot 参数校验全攻略:从基础到进阶
运维·服务器·spring boot·后端·百思可瑞教育·北京百思教育
武子康40 分钟前
大数据-89 Spark应用必备:进程通信、序列化机制与RDD执行原理
大数据·后端·spark