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:001 天前
Python进阶语法
开发语言·python
小康小小涵1 天前
睿抗机器人大赛魔力元宝
python·ubuntu·gitee·github
勇往直前plus1 天前
Python 类与实例对象的内存存储
java·开发语言·python
禾叙_1 天前
【canal】canal同步msyql到redis
android·redis·python
先做个垃圾出来………1 天前
Python位运算及操作
java·前端·python
一只大黄猫1 天前
【数据库-入门2】基本概念
数据库
人工小情绪1 天前
python报错:AttributeError: module ‘numpy‘ has no attribute ‘object‘.
python·numpy·neo4j
梦帮科技1 天前
第三十四篇:开源社区运营:GitHub Stars增长策略
开发语言·前端·爬虫·python·docker·架构·html
liu****1 天前
机器学习-线性回归
人工智能·python·算法·机器学习·回归·线性回归
阿蔹1 天前
Python-Pytest
python·自动化·pytest