【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

你可能感兴趣的:(Python2升版,django,python,数据库)