Django3.2安裝原始碼xadmin2步驟以及遇到的問題

七月and巨蟹發表於2024-05-15

使用xadmin2遇到的問題&解決

環境配置:

使用的模組版本:
img

關聯的包

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'

alt text
解決:將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'

alt text
解決:將 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'

alt text
解決:將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'

alt text
解決:註釋掉並改為:

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'

alt text
解決:註釋掉並改為:
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'

alt text
解決: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'

alt text
解決:這個問題我沒有找到解決辦法,不過從前面看只要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'

alt text
解決: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'

alt text
解決:註釋掉並改為:

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'

alt text
解決:註釋掉並改為:

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'

alt text
解決:同問題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'

alt text
解決:同問題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行
alt text
修改為:

from django.utils.functional import keep_lazy

解決2:同一個檔案下,第20行,匯入錯誤問題
alt text
修改為:

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行註釋掉即可,如下:
alt text

相關文章