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?

相关推荐
知识分享小能手1 小时前
Redis入门学习教程,从入门到精通,Redis 概述:知识点详解(1)
数据库·redis·学习
xixihaha13243 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
夕除3 小时前
Mysql--07
数据库·mysql
数据最前线3 小时前
5个瞬间,盘点国产数据库的2025年
数据库
jiankeljx3 小时前
Redis-配置文件
数据库·redis·oracle
xixihaha13243 小时前
Python游戏中的碰撞检测实现
jvm·数据库·python
Schengshuo4 小时前
SQL 中 COUNT 的用法详解
数据库·sql
顶点多余4 小时前
Mysql--后端与前端关系
数据库·mysql
mygljx4 小时前
【MySQL 的 ONLY_FULL_GROUP_BY 模式】
android·数据库·mysql
sunwenjian8864 小时前
Springboot项目本地连接并操作MySQL数据库
数据库·spring boot·mysql