使用xadmin2遇到的问题&解决
环境配置:
使用的模块版本:
关联的包
Django 3.2.15
mysqlclient 2.2.4
xadmin 2.0.1
django-crispy-forms >= 1.6.0
django-import-export >= 0.5.1
django-reversion >= 2.0.0
django-formtools == 2.1
future == 0.15.2
httplib2 == 0.9.2
six == 1.10.0
注意:
你需要安装的有:
Django=3.2.15
mysqlclient
xadmin
其中:xadmin我使用https方式安装的
pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2
xadmin下载链接:
https://github.com/sshwsfc/xadmin/tree/django2
使用:只需要其中的xadmin文件夹,放入项目中使用
问题&解决
问题1、ImportError: cannot import name 'six' from 'django.utils'
解决:在site-packages目录中找到six.py文件,将其复制到django/utils目录下。
问题2、ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding'
File "D:\Python310\.virtualenv\Wuwuchong\Lib\site-packages\xadmin\models.py", line 11, in <module>
from django.utils.encoding import python_2_unicode_compatible, smart_text
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding'
解决:将from django.utils.encoding import python_2_unicode_compatible, smart_text
注释掉并改为:
from django.utils.encoding import smart_text
from six import python_2_unicode_compatible
问题3、ImportError: cannot import name 'pretty_name' from 'django.forms.forms'
File "D:\Python310\.virtualenv\Wuwuchong\Lib\site-packages\xadmin\utils.py", line 7, in <module>
from django.forms.forms import pretty_name
ImportError: cannot import name 'pretty_name' from 'django.forms.forms'
解决:将 from django.forms.forms import pretty_name
注释掉并改为:
from django.utils import formats, six
问题4、ModuleNotFoundError: No module named 'django.contrib.staticfiles.templatetags'
File "D:\Python310\.virtualenv\Wuwuchong\Lib\site-packages\xadmin\utils.py", line 25, in <module>
from django.contrib.staticfiles.templatetags.staticfiles import static
ModuleNotFoundError: No module named 'django.contrib.staticfiles.templatetags'
解决:将25行注释掉并改为:
from django.templatetags.static import static
问题5、ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding'
File "D:\Python310\.virtualenv\Wuwuchong\Lib\site-packages\reversion\revisions.py", line 16, in <module>
from django.utils.encoding import force_text, python_2_unicode_compatible
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding'
解决:注释掉并改为:
from django.utils.encoding import force_text
from six import python_2_unicode_compatible
问题6、ModuleNotFoundError: No module named 'django.core.urlresolvers'
File "D:\Python310\.virtualenv\Wuwuchong\Lib\site-packages\reversion\admin.py", line 13, in <module>
from django.core.urlresolvers import reverse
ModuleNotFoundError: No module named 'django.core.urlresolvers'
解决:注释掉并改为:
from django.urls import reverse
问题7、ImportError: cannot import name 'BoundField' from 'django.forms.forms'
File "D:\Python310\.virtualenv\Wuwuchong\Lib\site-packages\crispy_forms\utils.py", line 9, in <module>
from django.forms.forms import BoundField
ImportError: cannot import name 'BoundField' from 'django.forms.forms'
解决:Django新版本BoundField位置移动了。注释掉并改为:
from django.forms.boundfield import BoundField
问题8、ImportError: cannot import name 'memoize' from 'django.utils.functional'
File "D:\Python310\.virtualenv\Wuwuchong\lib\site-packages\crispy_forms\compatibility.py", line 26, in <module>
from django.utils.functional import memoize
ImportError: cannot import name 'memoize' from 'django.utils.functional'
解决:这个问题我没有找到解决办法,不过从前面看只要24行没有报错就不会走到这里来。
百度一下,发现在Django 3.2中,django.utils.lru_cache.lru_cache模块已经被移除,因为Django 3.2中不再使用LRU缓存,而是改用内置的标准库中的functools.lru_cache。
将24行注释掉并改为:
from functools import lru_cache
问题9、ModuleNotFoundError: No module named 'django.core.urlresolvers'
File "D:\Python310\.virtualenv\Wuwuchong\lib\site-packages\crispy_forms\helper.py", line 4, in <module>
from django.core.urlresolvers import reverse, NoReverseMatch
ModuleNotFoundError: No module named 'django.core.urlresolvers'
解决:Django 3.x 版本reverse位置移动了,注释掉并改为:
from django.urls import reverse, NoReverseMatch
问题10、ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models.fields'
File "D:\Python310\.virtualenv\Wuwuchong\lib\site-packages\xadmin\plugins\filters.py", line 9, in <module>
from django.db.models.fields import FieldDoesNotExist
ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models.fields'
解决:注释掉并改为:
from django.core.exceptions import FieldDoesNotExist
问题11、ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models'
File "D:\Python310\.virtualenv\Wuwuchong\lib\site-packages\xadmin\plugins\aggregation.py", line 1, in <module>
from django.db.models import FieldDoesNotExist, Avg, Max, Min, Count, Sum
ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models'
解决:注释掉并改为:
from django.core.exceptions import FieldDoesNotExist
from django.db.models import Avg, Max, Min, Count, Sum
问题12、ModuleNotFoundError: No module named 'django.core.urlresolvers'
File "D:\Python310\.virtualenv\Wuwuchong\lib\site-packages\import_export\admin.py", line 16, in <module>
from django.core.urlresolvers import reverse
ModuleNotFoundError: No module named 'django.core.urlresolvers'
解决:同问题9,注释掉并改为:
from django.urls import reverse
问题13、ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models.fields'
File "D:\Python310\.virtualenv\Wuwuchong\lib\site-packages\import_export\resources.py", line 15, in <module>
from django.db.models.fields import FieldDoesNotExist
ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models.fields'
解决:同问题10,注释掉并改为:
from django.core.exceptions import FieldDoesNotExist
问题14、ImportError raised when trying to load 'crispy_forms.templatetags.crispy_forms_utils': cannot import name 'allow_lazy' from 'django.utils.functional'
File "D:\Python310\.virtualenv\Wuwuchong\lib\site-packages\django\template\backends\django.py", line 123, in get_package_libraries
raise InvalidTemplateLibrary(
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'crispy_forms.templatetags.crispy_forms_utils': cannot import name 'allow_lazy' from 'django.utils.functional'
解决1:需要修改site-packages\crispy_forms\templatetags\crispy_forms_utils.py文件第6行
修改为:
from django.utils.functional import keep_lazy
解决2:同一个文件下,第20行,导入错误问题
修改为:
remove_spaces = keep_lazy(remove_spaces, text_type)
问题15、TypeError: WidgetTypeSelect.render() got an unexpected keyword argument 'renderer'
File "D:\Python310\.virtualenv\Wuwuchong\lib\site-packages\django\forms\boundfield.py", line 93, in as_widget
return widget.render(
TypeError: WidgetTypeSelect.render() got an unexpected keyword argument 'renderer'
问题发生原因:进入xadmin后台,点击右上角增加用户小组件时,会报错
解决:查看虚拟环境下:Lib\site-packages\xadmin\views\dashboard.py,把97行注释掉即可,如下: