error总结
1.TypeError: __init__() missing 1 required positional argument: 'on_delete'
在django2.0后,定義外鍵和一對(duì)一關(guān)系的時(shí)候需要加on_delete選項(xiàng),此參數(shù)為了避免兩個(gè)表里的數(shù)據(jù)不一致問(wèn)題,不然會(huì)報(bào)錯(cuò):
TypeError: __init__() missing 1 required positional argument: 'on_delete'
舉例說(shuō)明:
user=models.OneToOneField(User)
owner=models.ForeignKey(UserProfile)
需要改成:
user=models.OneToOneField(User,on_delete=models.CASCADE) --在老版本這個(gè)參數(shù)(models.CASCADE)是默認(rèn)值
owner=models.ForeignKey(UserProfile,on_delete=models.CASCADE) --在老版本這個(gè)參數(shù)(models.CASCADE)是默認(rèn)值
參數(shù)說(shuō)明:
on_delete有CASCADE、PROTECT、SET_NULL、SET_DEFAULT、SET()五個(gè)可選擇的值
CASCADE:此值設(shè)置,是級(jí)聯(lián)刪除。
PROTECT:此值設(shè)置,是會(huì)報(bào)完整性錯(cuò)誤。
SET_NULL:此值設(shè)置,會(huì)把外鍵設(shè)置為null,前提是允許為null。
SET_DEFAULT:此值設(shè)置,會(huì)把設(shè)置為外鍵的默認(rèn)值。
SET():此值設(shè)置,會(huì)調(diào)用外面的值,可以是一個(gè)函數(shù)。
一般情況下使用CASCADE就可以了。
?OSError: No translation files found for default language zh-CN.
2.ImportError: cannot import name 'patterns'
這個(gè)特性在1.9就聲明了deprecated. 1.10正式移除了。使用 django 1.10 需要改用 django.conf.urls.url() 示范代碼:
from django.conf.urls import include, url from django.conf import settings# Uncomment the next two lines to enable the admin: from django.contrib.auth.decorators import login_required from django.contrib import admin admin.autodiscover() admin.site.login = login_required(admin.site.login) # 設(shè)置admin登錄的頁(yè)面,settings.LOGIN_URLimport forum.urls, api.urlsurlpatterns = [# Examples:# url(r'^$', 'xp.views.home', name='home'),# url(r'^xp/', include('xp.foo.urls')),# Uncomment the admin/doc line below to enable admin documentation:# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),# Uncomment the next line to enable the admin:# url(r'^admin/', include(admin.site.urls)),url(r'^', include(forum.urls)),url(r'^api/', include(api.urls)),url(r'^manage/admin/', include(admin.site.urls)), ]?3.regex = re.compile(ur'@(?P<username>\w+)(\s|$)', re.I)^? ? ? ??SyntaxError: invalid syntax
regex = re.compile('@(?P<username>\w+)(\s|$)', re.I)4.ImportError: No module named 'PIL'
C:\Users\zte>pip3 install pillow
Collecting pillow
Using cached https://files.pythonhosted.org/packages/b9/ba/43f2f2dd60f304d8563af82ecd4822ff0b57ddfd71631c407fce69da84d1/Pillow-5.4.1-cp35-cp35m-win_amd64.whl
Installing collected packages: pillow
Successfully installed pillow-5.4.1
?5.Django繼承AbstractUser新建User Model時(shí)出現(xiàn)fields.E304錯(cuò)誤
auth.User.groups: (fields.E304) Reverse accessor for ‘User.groups’ clashes with reverse accessor for ‘User.groups’.
HINT: Add or change a related_name argument to the definition for ‘User.groups’ or ‘User.groups’.
auth.User.user_permissions: (fields.E304) Reverse accessor for ‘User.user_permissions’ clashes with reverse accessor for ‘User.user_permissions’.
HINT: Add or change a related_name argument to the definition for ‘User.user_permissions’ or ‘User.user_permissions’.
users.User.groups: (fields.E304) Reverse accessor for ‘User.groups’ clashes with reverse accessor for ‘User.groups’.
HINT: Add or change a related_name argument to the definition for ‘User.groups’ or ‘User.groups’.
users.User.head_url: (fields.E210) Cannot use ImageField because Pillow is not installed.
HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command “pip install Pillow”.
users.User.user_permissions: (fields.E304) Reverse accessor for ‘User.user_permissions’ clashes with reverse accessor for ‘User.user_permissions’.
HINT: Add or change a related_name argument to the definition for ‘User.user_permissions’ or ‘User.user_permissions’.
解決方案:
需要在setting中重載AUTH_USER_MODEL
AUTH_USER_MODEL = 'users.UserProfile'users:你的app
UserProfile:model
6.LookupError: No installed app with label 'users'.
?
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','forum.apps.博客Config',]?
正確寫(xiě)法:
INSTALLED_APPS = ['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'forum.apps.博客Config',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'django.contrib.sitemaps', # Django sitemap framework
# 'forum',
'api',
'users',
]
AUTH_USER_MODEL ="users.UserProfile"
?
?7.django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: admin
創(chuàng)建的應(yīng)用中settings.py文件INSTALLED_APPS注冊(cè)文件按之前手動(dòng)自行注冊(cè)了應(yīng)用名稱(chēng)。
其實(shí)不需要注冊(cè)就好,更新django1.11.3后,django創(chuàng)建應(yīng)用的時(shí)候已經(jīng)幫你注冊(cè)了xx.apps.XXConfig。
?8.python reload(sys)找不到,name 'reload' is not defined
import importlib importlib.reload(sys) # reload(sys)?9.AttributeError: module 'sys' has no attribute 'setdefaultencoding
Python3字符串默認(rèn)編碼unicode,?所以sys.setdefaultencoding也不存在了
#sys.setdefaultencoding("utf8")?10.ImportError: No module named 'MySQLdb'
在 python2 中,使用?pip install mysql-python?進(jìn)行安裝連接MySQL的庫(kù),使用時(shí)?import MySQLdb?進(jìn)行使用
在 python3 中,改變了連接庫(kù),改為了 pymysql 庫(kù),使用pip install pymysql?進(jìn)行安裝,直接導(dǎo)入import pymysql使用
本來(lái)在上面的基礎(chǔ)上把 python3 的 pymysql 庫(kù)安裝上去就行了,但是問(wèn)題依舊
經(jīng)過(guò)查閱得知, Django 依舊是使用 py2 的 MySQLdb 庫(kù)的,得到進(jìn)行適當(dāng)?shù)霓D(zhuǎn)換才行
在__init__.py?文件中添加以下代碼
import pymysql pymysql.install_as_MySQLdb()11. OSError: No translation files found for default language zh-CN.
IOError: No translation files found for default language zh-cn.
?
檢查 ?...\Lib\site-packages\Django-1.10.2-py2.7.egg\django\conf\locale下無(wú)zh-cn文件夾,有zh-Hans和zh-Hant兩個(gè)文件,
其中?zh-Hans是簡(jiǎn)體中文 ? ?zh-Hant是繁體中文
?
所以更改setttings.py 下?
LANGUAGE_CODE = 'zh-Hans'即可
?
12.error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
解決方法:下載?VCForPython27.msi 。
地址:?http://www.microsoft.com/en-us/download/confirmation.aspx?id=44266
?(Microsoft Visual C++ Compiler for Python 2.7)
?
?
13.error: command 'C:\\Users\\zte\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2
_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
error: command 'C:\\Users\\zte\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2
?
注意:python2.x用mysql-python,從Python3.x起,變更為mysqlclient
pip install mysql-python到?http://www.lfd.uci.edu/~gohlke/pythonlibs/?下載二進(jìn)制安裝包
pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
轉(zhuǎn)載于:https://www.cnblogs.com/noplablem-wangzhe0635/p/10021652.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
- 上一篇: 高仿网易新闻频道选择器
- 下一篇: Django modules模块