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?

相关推荐
AI Echoes2 分钟前
构建一个LangChain RAG应用
数据库·python·langchain·prompt·agent
@nengdoudou33 分钟前
KingbaseES支持 mysql 的find_in_set函数
数据库·mysql
派大鑫wink40 分钟前
从零到精通:Python 系统学习指南(附实战与资源)
开发语言·python
c骑着乌龟追兔子43 分钟前
Day 38 官方文档的阅读
python
摇滚侠1 小时前
面试实战 问题三十三 Spring 事务常用注解
数据库·spring·面试
梁萌1 小时前
保姆级的MySQL执行计划(Explain)解读
数据库·mysql·explain·执行计划
JIngJaneIL1 小时前
基于Java+ vue智慧医药系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
羸弱的穷酸书生1 小时前
国网 i1协议 python实现
开发语言·python
weixin_462022351 小时前
RAW-Adapter: Adapting Pre-trained Visual Model to Camera RAW Images
python·计算机视觉
电子硬件笔记1 小时前
Python语言编程导论第三章 编写程序
开发语言·python·编辑器