error总结
1.TypeError: __init__() missing 1 required positional argument: 'on_delete'
在django2.0后,定義外鍵和一對一關系的時候需要加on_delete選項,此參數為了避免兩個表里的數據不一致問題,不然會報錯:
TypeError: __init__() missing 1 required positional argument: 'on_delete'
舉例說明:
user=models.OneToOneField(User)
owner=models.ForeignKey(UserProfile)
需要改成:
user=models.OneToOneField(User,on_delete=models.CASCADE) --在老版本這個參數(models.CASCADE)是默認值
owner=models.ForeignKey(UserProfile,on_delete=models.CASCADE) --在老版本這個參數(models.CASCADE)是默認值
參數說明:
on_delete有CASCADE、PROTECT、SET_NULL、SET_DEFAULT、SET()五個可選擇的值
CASCADE:此值設置,是級聯刪除。
PROTECT:此值設置,是會報完整性錯誤。
SET_NULL:此值設置,會把外鍵設置為null,前提是允許為null。
SET_DEFAULT:此值設置,會把設置為外鍵的默認值。
SET():此值設置,會調用外面的值,可以是一個函數。
一般情況下使用CASCADE就可以了。
?OSError: No translation files found for default language zh-CN.
2.ImportError: cannot import name 'patterns'
這個特性在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) # 設置admin登錄的頁面,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時出現fields.E304錯誤
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',]?
正確寫法:
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
創建的應用中settings.py文件INSTALLED_APPS注冊文件按之前手動自行注冊了應用名稱。
其實不需要注冊就好,更新django1.11.3后,django創建應用的時候已經幫你注冊了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字符串默認編碼unicode,?所以sys.setdefaultencoding也不存在了
#sys.setdefaultencoding("utf8")?10.ImportError: No module named 'MySQLdb'
在 python2 中,使用?pip install mysql-python?進行安裝連接MySQL的庫,使用時?import MySQLdb?進行使用
在 python3 中,改變了連接庫,改為了 pymysql 庫,使用pip install pymysql?進行安裝,直接導入import pymysql使用
本來在上面的基礎上把 python3 的 pymysql 庫安裝上去就行了,但是問題依舊
經過查閱得知, Django 依舊是使用 py2 的 MySQLdb 庫的,得到進行適當的轉換才行
在__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下無zh-cn文件夾,有zh-Hans和zh-Hant兩個文件,
其中?zh-Hans是簡體中文 ? ?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/?下載二進制安裝包
pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
轉載于:https://www.cnblogs.com/noplablem-wangzhe0635/p/10021652.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: 高仿网易新闻频道选择器
- 下一篇: Django modules模块