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

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

相关推荐
ID_180079054733 小时前
基于 Python 的 Cdiscount 商品详情 API 调用与 JSON 核心字段解析(含多规格 SKU 提取)
开发语言·python·json
闲人编程3 小时前
OpenAPI/Swagger规范与API文档自动化
运维·自动化·json·swagger·schema·openapi·codecapsule
Q_Q5110082854 小时前
python+django/flask+vue的大健康养老公寓管理系统
spring boot·python·django·flask·node.js
我是哈哈hh4 小时前
【Python数据分析】Numpy总结
开发语言·python·数据挖掘·数据分析·numpy·python数据分析
Michelle80234 小时前
24大数据 14-2 函数练习
开发语言·python
qq_381454994 小时前
Python学习技巧
开发语言·python·学习
Ndmzi4 小时前
Matlab编程技巧:自定义Simulink菜单(理解补充)
前端·javascript·python
dagouaofei5 小时前
AI生成个性化年终总结PPT
人工智能·python·powerpoint
white-persist6 小时前
VSCode 快捷键大全:从设计理念到场景化高效运用(详细解析)(文章末尾有vim快捷键大全)
linux·ide·vscode·python·编辑器·系统安全·vim
liliangcsdn6 小时前
python threading异步并发调用示例
python