python(Django)自动化之链接数据库

1、安装数据库

具体安装mysql的教程其实在csdn上都有具体教程大家可以看看

2、更换django的配置

具体是在settings.py中将代码更改成如下:

复制代码
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'autotest',
        'USER': 'root',
        'PASSWORD': 'chengxian8.8',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}
注意:是更改不是添加

3、更改__init__.py内容

复制代码
import pymysql
pymysql.install_as_MySQLdb()

4、安装PyMySQL模块

在命令行模式中更换到PyMySQL所在目录下进行安装

pip install PyMySQL

5、迁移同步数据库和表结构

(.venv) PS D:\python project(1)\my_django_2> python manage.py makemigrations

No changes detected

(.venv) PS D:\python project(1)\my_django_2> 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

这个是操作及结果

6、创建数据库超级管理员

(.venv) PS D:\python project(1)\my_django_2> python manage.py createsuperuser

用户名: root

电子邮件地址: chengxian728.8@outlook.com

Password:

7、查看结果

启动服务后在web上输入127.0.0.1:8000/admin

然后登陆你设置的账户密码即可

相关推荐
Dream of maid1 分钟前
Python基础 6 (面向对象)
开发语言·python
郝学胜-神的一滴4 分钟前
「栈与缩点的艺术」二叉树前序序列化合法性判定:从脑筋急转弯到工程实现
java·开发语言·数据结构·c++·python·算法
无心水14 分钟前
22、Java开发避坑指南:日期时间、Spring核心与接口设计的最佳实践
java·开发语言·后端·python·spring·java.time·java时间处理
Hello.Reader19 分钟前
双卡 A100 + Ollama 最终落地手册一键部署脚本、配置文件、预热脚本与 Python 客户端完整打包
开发语言·网络·python
vx_biyesheji000119 分钟前
计算机毕业设计:Python网约车订单数据可视化系统 Django框架 可视化 数据大屏 数据分析 大数据 机器学习 深度学习(建议收藏)✅
大数据·python·机器学习·信息可视化·django·汽车·课程设计
AC赳赳老秦24 分钟前
OpenClaw实战案例:用1个主控+3个Agent,实现SEO文章日更3篇
服务器·数据库·python·mysql·.net·deepseek·openclaw
智算菩萨27 分钟前
PyCharm版本发展史:从诞生到AI时代的Python IDE演进历程
ide·人工智能·python·pycharm·ai编程
Khsc434ka30 分钟前
LeetCode-001:Python 实现哈希表求两数之和:初识哈希表
python·leetcode·散列表
AC赳赳老秦32 分钟前
OpenClaw阿里云部署实操:多Agent协同,打造云端自动化工作流
人工智能·阿里云·数据挖掘·自动化·云计算·deepseek·openclaw
冬至喵喵1 小时前
构建 CLI 的 Python 框架:Typer技术介绍
开发语言·chrome·python