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

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

相关推荐
民乐团扒谱机9 分钟前
【附完整代码】Python爬取古筝网曲谱图片一键生成PDF(下·PDF生成与GUI篇)
开发语言·python·pdf
lunareclipse11 分钟前
Python 填坑:消失的信号点 —— 详解“可变默认参数”陷阱
python
光之后裔13 分钟前
Numpy以及Pytorch中多维数组的维度数与维度值以及轴axis理解
pytorch·python·numpy
tangweiguo0305198719 分钟前
RAG 从零到一:让大模型读懂你的文档
python·langchain
挖AI金矿21 分钟前
(六)文件与搜索 - 信息处理的正确姿势
人工智能·python·开源·个人开发·ai编程
oioihoii22 分钟前
OpenClaw桌面 UI 自动化中的 Token 消耗问题几种可能的优化方向
运维·ui·自动化
Fleshy数模26 分钟前
Python+MediaPipe 实现实时手部关键点检测(新手避坑完整版)
python
b***251127 分钟前
18650与21700电芯在锂电池自动化生产线中的协同发展
运维·自动化
2401_8330336230 分钟前
c++如何实现简单的文件签名验证_HMAC-SHA1算法应用【进阶】
jvm·数据库·python
重庆若鱼文化创意39 分钟前
包装设计公司哪家好?价格差很多时,关键看材质、印刷工艺和实际包装成本
人工智能·python·材质