django项目配置xadmin,版本不对问题的处理

针对django 2.0 xadmin 错误集锦

  1. ModuleNotFoundError: No module named 'django.core.urlresolvers'
解决方法:将from django.core.urlresolvers import NoReverseMatch, reverse
改成
from django.urls import NoReverseMatch, reverse
2.TypeError: __init__() missing 1 required positional argument: 'on_delete'
解决方法:将user = models.ForeignKey(AUTH_USER_MODEL, verbose_name=_(u"user"))
改成
user = models.ForeignKey(AUTH_USER_MODEL, verbose_name=_(u"user"), on_delete=models.CASCADE)
即在外键值的后面加上 on_delete=models.CASCADE
3.TypeError: __init__() takes 1 positional argument but 6 were given
解决方法:将forms.Field.__init__(self, required, widget, label, initial, help_text, *args, **kwargs)
改成
forms.Field.__init__(self,*args, **kwargs)
4.ImportError: cannot import name 'QUERY_TERMS'
解决方法:将from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS
改成
from django.db.models.sql.query import LOOKUP_SEP
from django.db.models.sql.constants import QUERY_TERMS
5.AttributeError: 'Settings' object has no attribute 'MIDDLEWARE_CLASSES'
解决方法:将if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE_CLASSES:
改成
if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE:
6.
django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1")
原因:
gender = models.CharField('性别',max_length=30,choices=(('male','男'),('female','女')),default='female')
可以看到设置的字符长度是5,可是female有6个字节,所以修改max_length=7
注:必须重新生成数据库
python manage.py makemigrations
python manage.py migrate
7.init() got an unexpected keyword argument 'current_app'
解决方法:这里面说是要激活中间件必须给他添加一个中间件class ,即MIDDLEWARE_CLASS
先把MIDDLEWARE
改成MIDDLEWARE_CLASS
8.AttributeError: 'WSGIRequest' object has no attribute 'user'
解决方法:MIDDLEWARE_CLASS = [
'django.middleware.security.SecurityMiddleware',1
'django.contrib.sessions.middleware.SessionMiddleware',2
'django.middleware.common.CommonMiddleware',3
'django.middleware.csrf.CsrfViewMiddleware',4
'django.contrib.auth.middleware.AuthenticationMiddleware',5
'django.contrib.messages.middleware.MessageMiddleware',6
'django.middleware.clickjacking.XFrameOptionsMiddleware',7
]
9.ImportError: cannot import name 'login' from 'django.contrib.auth.views'
解决方案:
from django.contrib.auth.views import auth_login
from django.contrib.auth.views import auth_logout
然后再把,方法里面的引用也改一下
10.ImportError: cannot import name 'QUERY_TERMS' from 'django.db.models.sql.query'
# django2.1.1版本将xadmin\plugins\filters.py文件中的
from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS

# 修改为
from django.db.models.sql.query import LOOKUP_SEP, Query


# 在Django2.0版本中把
from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS

# 修改为:
from django.db.models.sql.query import LOOKUP_SEP
from django.db.models.sql.constants import QUERY_TERMS
11.ModuleNotFoundError: No module named 'django.contrib.formtools'
解决方案:
pip install django.formtools -i https://mirrors.aliyun.com/pypi/simple
12.ImportError: cannot import name 'password_reset_confirm' from 'django.contrib.auth.views'
# 把xadmin\plugins\password.py中的
from django.contrib.auth.views import password_reset_confirm

修改为:
from django.contrib.auth.views import PasswordResetConfirmView

其实当你阅读完这篇文章之后,我还想告诉你,不用这样处理,直接安装下载最新版本的xadmin就行了,直接解决问题。
pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2
还是按照原来的配置,主要要找到你的pip安装第三方库的路径就可以了。我的是在C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages下。
最后python manage.py runserver 运行
django项目配置xadmin,版本不对问题的处理
文章图片

【django项目配置xadmin,版本不对问题的处理】

    推荐阅读