Django之配置数据库

一,创建项目

二,将项目的setting.py中的

复制代码
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

替换成如下(以mysql为例)

复制代码
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your database',
        'HOST': 'your host',
        'PORT': 'your port',
        'USER': 'your name,
        'PASSWORD': 'your passwd',
    }
}

三,在项目的__init__.py中加入如下

复制代码
import pymysql
pymysql.install_as_MySQLdb()

否则会报如下错误

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.

Did you install mysqlclient?

相关推荐
花酒锄作田5 小时前
Pydantic校验配置文件
python
hboot6 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
GBASE10 小时前
G术时刻 |GBase 8s数据库事务并发控制之封锁技术介绍(下)
数据库
ZhengEnCi16 小时前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi18 小时前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽18 小时前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户83580861879119 小时前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
xiezhr20 小时前
逛GitHub发现了一款免费的带AI功能的数据库管理工具
数据库·ai编程·dba
Warson_L1 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅1 天前
海天线算法的前世今生
python·计算机视觉