从django1.x 升级至最新版本 django2.x

用pip进行更新后


pip install --upgrade Django==2.2
1
一堆报错
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解决方法:
找到 python目录下/site-packages/django/db/backends/mysql/base.py,注释掉下方的两行代码


TypeError: init() missing 1 required positional argument: ‘on_delete’
解决方法:
表与表之间关联的时候,必须要写on_delete参数


on_delete参数的各个值的含义:

on_delete=None, # 删除关联表中的数据时,当前表与其关联的field的行为
on_delete=models.CASCADE, # 删除关联数据,与之关联也删除
on_delete=models.DO_NOTHING, # 删除关联数据,什么也不做
on_delete=models.PROTECT, # 删除关联数据,引发错误ProtectedError

# models.ForeignKey('关联表', on_delete=models.SET_NULL, blank=True, null=True)
on_delete=models.SET_NULL, # 删除关联数据,与之关联的值设置为null(前提
FK字段需要设置为可空,一对一同理)

# models.ForeignKey('关联表', on_delete=models.SET_DEFAULT, default='默认值')
on_delete=models.SET_DEFAULT, # 删除关联数据,与之关联的值设置为默认值(前提
FK字段需要设置默认值,一对一同理)

on_delete=models.SET, # 删除关联数据,
a. 与之关联的值设置为指定值,设置:models.SET(值)
b. 与之关联的值设置为可执行对象的返回值,设置:models.SET(可执行对象)


ImportError: cannot import name ‘RegexURLResolver’ from ‘django.urls’ (D:\Anaconda3\envs\scrapy\lib\site-packages\django\urls_init_.py)

解决方式:把下面注释掉的代码换为第二行的代码
# from django.urls import RegexURLResolver, RegexURLPattern
from django.urls.resolvers import URLResolver, URLPattern

django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

解决方法:django.urls中include函数第一个参数传入的是tuple类型,其中一个是参数是app_name,且app_name需赋值,在include中改为下面的代码
include(('rbac.urls','rbac'), namespace='rbac')

query = query.decode(errors=‘replace’)AttributeError: ‘str’ object has no attribute ‘decode’
解决方法:
把decode改为encode

文字版:

先升级 pip install --upgrade django
然后运行 python manage.py check
遇到问题解决问题

E01
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

找到 ~/.virtualenvs/arryblog/lib/python3.6/site-packages/django/db/backends/mysql/base.py
将文件中的如下代码注释
if version < (1, 3, 3):
raise ImproperlyConfigured(“mysqlclient 1.3.3 or newer is required; you have %s” % Database.version)

E02
TypeError: init() missing 1 required positional argument: ‘on_delete’

使用编辑器的正则替换
Find: models.ForeignKey((.+))
Replace: models.ForeignKey($1,on_delete=models.CASCADE)

E03
ModuleNotFoundError: No module named ‘django.core.urlresolvers’

使用编辑器的替换
Find: from django.core.urlresolvers import
Replace: from django.urls import

E04
django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported. Pass a 2-tuple containing the list of patterns and app_name, and provide the namespace argument to include() instead.

修改
url(r’^admin/’, include(admin.site.urls)),

url(r’^admin/’, admin.site.urls),

E05
'Specifying a namespace in include() without providing an app_name ’
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

在每个 urls.py 里添加 app_name = ‘’

E06
File “/home/arry/.virtualenvs/arryblog/lib/python3.6/site-packages/django_comments/init.py”, line 2, in
from django.core import urlresolvers

ImportError: cannot import name ‘urlresolvers’

第三方包django-contrib-comments版本太低 运行
pip install --upgrade django-contrib-comments

E07
django.core.cache.backends.base.InvalidCacheBackendError: Could not find backend ‘django_redis.cache.RedisCache’: cannot import name ‘get_cache’

没有找到django-redis
先升级 pip install --upgrade django-redis

E08
?: (admin.E408) ‘django.contrib.auth.middleware.AuthenticationMiddleware’ must be in MIDDLEWARE in order to use the admin application.
?: (admin.E409) ‘django.contrib.messages.middleware.MessageMiddleware’ must be in MIDDLEWARE in order to use the admin application.

修改 setting.py 里的 MIDDLEWARE_CLASSES 为 MIDDLEWARE

W09
WARNINGS:
?: (urls.W005) URL namespace ‘user’ isn’t unique. You may not be able to reverse all URLs in this namespace

同时定义了两个叫做 user 的 url
url(r’^’, include(‘user.urls’,namespace=‘user’)), # 用户模块
url(r’^user/’, include(‘user.urls’,namespace=‘user’)), # 用户模块
删除一个

最后检查
python manage.py check
没有问题
运行

E10
(‘type’, models.ForeignKey(verbose_name=‘照片类型’, to=‘photo.Album’,on_delete=models.CASCADE),
SyntaxError: invalid syntax

查找替换
Find: ),on_delete=models.CASCADE),
Replace: ,on_delete=models.CASCADE)),

E11
File “/home/arry/.virtualenvs/arryblog/lib/python3.6/site-packages/django/db/backends/mysql/operations.py”, line 146, in last_executed_query
query = query.decode(errors=‘replace’)
AttributeError: ‘str’ object has no attribute ‘decode’

修改 decode 为 encode
vi /home/arry/.virtualenvs/arryblog/lib/python3.6/site-packages/django/db/backends/mysql/operations.py

定位
:146

E12
AttributeError: module ‘django.contrib.auth.middleware’ has no attribute ‘SessionAuthenticationMiddleware’

取消了 SessionAuthenticationMiddleware (怎么进行session认证呢?)
删除 django.contrib.auth.middleware.SessionAuthenticationMiddleware

E13
You have 8 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, sites.
Run ‘python manage.py migrate’ to apply them.

刚才修改过程修改了 migration
使用 python manage.py migrate --fake

python manage.py runserver
服务器运行正常

E14
TypeError at /
‘bool’ object is not callable

原因使用了
if request.user.is_authenticated():
查找替换 user.is_authenticated() 为 user.is_authenticated

从django 1.8 升级至 django2.2 大功告成

posted @ 2021-05-08 20:07  笑而不语心自闲  阅读(425)  评论(0编辑  收藏  举报