【Python2转换升级Python3异常错误更改】(三)

目录

22.ImportError: cannot import name 'force_unicode' from 'django.utils.encoding'

23.ImportError: cannot import name 'curry' from 'django.utils.functional'

24.ModuleNotFoundError: No module named 'apscheduler'

25.ModuleNotFoundError: No module named 'httplib'

26.AttributeError: module 'sys' has no attribute 'maxint'

27.NameError: name 'long' is not defined

28.TypeError: init() missing 1 required positional argument: 'on_delete'

29.django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

30.ModuleNotFoundError: No module named 'statici18n'

31.RuntimeError: Model class djproject.models.TNsaeSslHost doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

32.ImportError: cannot import name 'unquote' from 'urllib'


22.ImportError: cannot import name 'force_unicode' from 'django.utils.encoding'

解决:
python3不支持force_unicode 名字改为 force_text

23.ImportError: cannot import name 'curry' from 'django.utils.functional'

解决:
curry 在python3.0里不支持 使用
from functools import partialmethod
替换

24.ModuleNotFoundError: No module named 'apscheduler'

解决:
pip3 install apscheduler

25.ModuleNotFoundError: No module named 'httplib'

解决:
python2中的httplib在python3中直接用http.client替换

26.AttributeError: module 'sys' has no attribute 'maxint'

解决:
将
sys.maxint
改为
sys.maxsize

27.NameError: name 'long' is not defined

解决:
long 换成 int
try:
    long
except NameError:
    long = int

28.TypeError: init() missing 1 required positional argument: 'on_delete'

解决:
在定义外键的时候加上"on_delete="
(1)、on_delete = None:
删除关联表的数据时,当前表与关联表的filed的行为。
(2)、on_delete = models.CASCADE:
表示级联删除,当关联表(子表)中的数据删除时,与其相对应的外键(父表)中的数据也删除。
(3)、on_delete = models.DO_NOTHING:
你删你的,父亲(外键)不想管你
(4)、on_delete = models.PROTECT:
保护模式,如采用这个方法,在删除关联数据时会抛出ProtectError错误
(5)、on_delete = models.SET_DEFAULT:
设置默认值,删除子表字段时,外键字段设置为默认值,所以定义外键的时候注意加上一个默认值。
(6)、on_delete = models.SET(值):
删除关联数据时,自定义一个值,该值只能是对应指定的实体
(7)、on_delete = models.SET_NULL
置空模式,删除时,外键字段被设置为空,前提就是blank=True, null=True,定义该字段时,允许为空。理解:删除关联数据(子表),与之关联的值设置默认值为null(父表中),这个前提需要父表中的字段可以为空。

29.django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

解决:
settings.py 解除注释
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'netcvm',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': 'Infosec@2019',                  # Not used with sqlite3.
        #'HOST': '10.80.11.54',                      # Set to empty string for localhost. Not used with sqlite3.
        'HOST': '127.0.0.1',
        'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.
    }
}
在manage.py新增
   django.setup()

30.ModuleNotFoundError: No module named 'statici18n'

解决:
pip3 install django-statici18n

31.RuntimeError: Model class djproject.models.TNsaeSslHost doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

解决:
settings.py解除注释
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
​
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'statici18n',#DEVMODE - for generating javascript i18n files
    'djproject' # 新增
)

32.ImportError: cannot import name 'unquote' from 'urllib'

解决:
from urllib import unquote
改为
from urllib.parse import unquote

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值