Django建站過程遇到的問題

1 Django和MySQL不兼容:

【現象】:在命令行中輸入py manage.py runserver後提示出如下錯誤

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.

這是因爲Django對於mysql是不兼容的,而且你應該已經下載了pymysql

【解決】:

  1. 首先確定你在__init.py__文件中有輸入如下代碼:
import pymysql
pymysql.install_as_MySQLdb()
  1. 其次找到出現問題的文件
    在這裏插入圖片描述
    就是錯誤提示中的base.py文件,然後對這個文件進行編輯。找到如下語句,然後將這個if語句註釋掉就可以了。
# 之前的代碼
version = Database.version_info
if version < (1, 3, 13):
    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

# 修改之後的代碼:
version = Database.version_info
# if version < (1, 3, 13):
#     raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

2 URL中沒東西

【現象】:

raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module '....urls' from '...'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

這是由於你的urls.py文件中沒有設置任何的路由導致的,需要在其中簡答設置一個路由。

3 decode問題

【現象】:

File "C:\Users\username\AppData\Local\Programs\Python\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'

【解決】:

#原代碼:
query = getattr(cursor, '_executed', None)
if query is not None:
     query = query.decode(errors='replace')
return query
 
#修改爲:
query = getattr(cursor, '_executed', None)
if query is not None:
     query = query.encode(errors='replace')
return query

即,將decode修改爲encode,此解決方法參考在Django框架中偶遇報錯:AttributeError: ‘str’ object has no attribute ‘decode’解決辦法_亭有枇杷樹

4 Django的admin密碼問題

在初始的admin管理界面,需要我們自己設定賬號和密碼,具體流程如下:
Django初始化話admin賬號和密碼_NTopic

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章