使用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-r ...
使用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行註釋掉即可,如下: