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_URL

import forum.urls, api.urls

urlpatterns = [
    # 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。

 
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',
]

 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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uncaught Error是指在JavaScript中发生的未捕获的错误。当代码中出现错误但没有被try-catch块或错误处理函数捕获时,就会抛出Uncaught Error。这种错误会导致代码执行中断,并在浏览器的控制台中显示错误信息。 Uncaught Error的原因可能有很多,例如语法错误、逻辑错误、网络请求失败等。为了避免出现Uncaught Error,我们可以采取以下几种解决方案: 1. 使用try-catch块捕获错误:通过将可能出现错误的代码放在try块中,并使用catch块来处理错误,可以避免出现未捕获的错误。例如: ```javascript try { // 可能出现错误的代码 } catch (error) { // 错误处理逻辑 } ``` 2. 使用错误处理函数:在JavaScript中,可以使用window.onerror全局事件处理函数来捕获未捕获的错误。该函数会在发生未捕获的错误时被调用,并可以用于记录错误信息或进行其他处理。例如: ```javascript window.onerror = function(message, source, lineno, colno, error) { // 错误处理逻辑 }; ``` 3. 使用Promise的catch方法:如果代码中使用了Promise对象,可以使用catch方法来捕获Promise中的错误。catch方法会在Promise链中的任何位置捕获错误,并执行相应的错误处理逻辑。例如: ```javascript promise.catch(function(error) { // 错误处理逻辑 }); ``` 总结一下,Uncaught Error是指在JavaScript中发生的未捕获的错误。为了避免出现这种错误,我们可以使用try-catch块、错误处理函数或Promise的catch方法来捕获和处理错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值