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

电子邮件地址: [email protected]

Password:

7、查看结果

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

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

相关推荐
PWRJOY1 分钟前
Flask-SQLAlchemy_数据库配置
数据库·python·flask
mahuifa17 分钟前
Qt图表绘制(QtCharts)- 性能优化(13)
python·qt·pyside6·开发经验·qtchart
Bugabooo40 分钟前
python打卡DAY22
开发语言·python
宇钶宇夕1 小时前
MODBUS RTU通信协议详解与调试指南
运维·程序人生·自动化
低维歌者1 小时前
python训练营day27
java·开发语言·python
微刻时光1 小时前
影刀处理 Excel:智能工具带来的高效变革
人工智能·python·低代码·自动化·excel·rpa·影刀rpa
大帅不是我1 小时前
Python多进程编程执行任务
java·前端·python
Fu_lucas1 小时前
Python Logging 模块完全指南
开发语言·python
Eiceblue1 小时前
Python 在Excel单元格中应用多种字体样式
开发语言·vscode·python·pycharm·excel
Superstarimage3 小时前
使用conda创建python虚拟环境,并自定义路径
windows·python·conda