目錄
- 問題一:更改數(shù)據(jù)庫(kù)
- 問題二:No module named 'MySQLdb'
- 問題三:
- 問題四:
- 問題五:
問題一:更改數(shù)據(jù)庫(kù)
django默認(rèn)使用sqlite的數(shù)據(jù)庫(kù),默認(rèn)自帶sqlite的數(shù)據(jù)庫(kù)驅(qū)動(dòng) , 引擎名稱:django.db.backends.sqlite3,如果我們需要使用MySQL的話,那么我們需要手動(dòng)修改settings.py的內(nèi)容
解決辦法:
找到DATABASES,將數(shù)據(jù)庫(kù)引擎修改為MySql。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'books', #你的數(shù)據(jù)庫(kù)名稱
'USER': 'root', #你的數(shù)據(jù)庫(kù)用戶名
'PASSWORD': '', #你的數(shù)據(jù)庫(kù)密碼
'HOST': '', #你的數(shù)據(jù)庫(kù)主機(jī),留空默認(rèn)為localhost
'PORT': '3306', #你的數(shù)據(jù)庫(kù)端口
}
}
問題二:No module named ‘MySQLdb’
這是因?yàn)閐jango默認(rèn)你導(dǎo)入的驅(qū)動(dòng)是MySQLdb,可是MySQLdb對(duì)于py3有很大問題,所以我們需要的驅(qū)動(dòng)是PyMySQL
所以,我們只需要找到項(xiàng)目名文件下的__init__,在里面寫入:
解決辦法:
import pymysql
pymysql.install_as_MySQLdb()
問題三:
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解決辦法:
第一步找到本機(jī)安裝python的目錄
C:\Python37\Lib\site-packages\django\db\backends\mysql(python安裝目錄)打開base.py,注釋掉以下內(nèi)容:
if version < (1, 3, 13):
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
問題四:
File "C:\Python37\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_query
query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
解決辦法:
解決辦法:打開此文件把146行的decode修改為encode
問題五:
試用python3.7 + Django2.1.3的時(shí)候, 在創(chuàng)建模型類關(guān)聯(lián)外鍵時(shí), 報(bào)如下錯(cuò)誤:
Traceback (most recent call last):
xxxxxxxxxxx
TypeError: __init__() missing 1 required positional argument: 'on_delete'
解決辦法:
# 關(guān)聯(lián)外鍵時(shí)設(shè)置`on_delete=`參數(shù)即可
student_grade = models.ForeignKey('Grades', on_delete=models.CASCADE)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
