Django3 使用xadmin

xadmin下载地址:https://github.com/vip68/xadmin_bugfix

下载完之后解压,只需要把里面的xadmin文件夹和requirements.txt文件复制到项目根目录下,然后在终端执行

pip install -r requirements.txt

执行完之后,去项目中的settings.py中注册xamin等应用模块

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'home',
    'corsheaders',

    # xadmin后台
    'xadmin',
    'crispy_forms',
    'reversion',
]

之后,将模块中用到的数据迁移到数据库,这里要先保证已经有连接数据库了。

python manage.py migrate

控制台会报错如下

  File "K:\Program Files\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "K:\Python_day\s32\py32\xadmin\plugins\importexport.py", line 48, in <module>
    from import_export.admin import DEFAULT_FORMATS, SKIP_ADMIN_LOG, TMP_STORAGE_CLASS
ImportError: cannot import name 'SKIP_ADMIN_LOG' from 'import_export.admin' (K:\Python_day\s32\py32\venv\lib\site-packages\import_export\admin.py)

点击报错的链接: File "K:\Python_day\s32\py32\xadmin\plugins\importexport.py", line 48, in <module>

打开importexport.py,在def get_skip_admin_log(self)的上面加上test(self)函数代码保存

      def test(self):
        if self.aaa is None:
            return ImportMixin(ImportExportMixin).get_skip_admin_log()
        else:
            return self.aaa
 
    # def get_skip_admin_log(self):
    #     if self.skip_admin_log is None:
    #         return SKIP_ADMIN_LOG
    #     else:
    #         return self.skip_admin_log
    #
    # def get_tmp_storage_class(self):
    #     if self.tmp_storage_class is None:
    #         return TMP_STORAGE_CLASS
    #     else:
    #         return self.tmp_storage_class

 导入ImportMixin, ImportExportMixin

from import_export.admin import DEFAULT_FORMATS, ImportMixin, ImportExportMixin

终端执行:

python manage.py makemigrations

python manage.py migrate

修改项目下的url.py文件中的后台访问路径,导入包之后访问http://127.0.0.1:8000/admin/

urlpatterns = [
    path('admin/', xadmin.site.urls),
}

setting中修改成中文界面

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/shanghai'

参考原文链接:django3.0 + python 3.8 + xadmin后台更改_花开花落与云卷云舒的博客-CSDN博客

posted @ 2023-01-28 09:18  笑而不语心自闲  阅读(231)  评论(0编辑  收藏  举报