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

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

相关推荐
djjdjdjdjjdj几秒前
Python Selenium怎么定位元素_By.XPATH与By.CSS_SELECTOR操作DOM节点
jvm·数据库·python
CHENKONG_CK12 分钟前
智流链驱动 RFID 混流装配,赋能汽车精益生产
网络·人工智能·tcp/ip·自动化·射频工程·rfid
m0_4939345314 分钟前
C#怎么实现EF Core全局查询过滤 C#如何用HasQueryFilter配置全局过滤条件自动排除已删除数据【数据库】
jvm·数据库·python
AI_大白17 分钟前
Python + Redis 实时行情共享:WebSocket 数据流的订阅管理与断线恢复实践
python·架构
财经资讯数据_灵砚智能18 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月19日
人工智能·python·信息可视化·语言模型·自然语言处理·知识图谱·ai编程
.柒宇.19 分钟前
Python 运维实战:psutil 监控系统资源 + paramiko 远程管理服务器
运维·服务器·python
几度热忱19 分钟前
【uv包管理工具】常用命令记录
python·uv
weixin_5689960619 分钟前
处理大体积DBF文件导入卡顿怎么办_性能优化与分批操作
jvm·数据库·python
xcbrand23 分钟前
工业制造品牌全案公司哪家专业
大数据·人工智能·python·制造
m0_6403093026 分钟前
如何处理SQL查询中的逻辑重叠:AND OR嵌套优先级
jvm·数据库·python