亲宝软件园·资讯

展开

django安装xadmin

angfulin 人气:0

环境:

一,安装

这里我选择从 GitHub 安装:

pip install git+https://github.com/sshwsfc/xadmin.git

结果如下:

Successfully installed 
defusedxml-0.7.1 
diff-match-patch-20200713 
django-crispy-forms-1.11.2 
django-formtools-2.3 
django-import-export-2.5.0 
django-reversion-3.0.9 
et-xmlfile-1.1.0 future-0.18.2 
httplib2-0.9.2 
markuppy-1.14 
odfpy-1.4.1 
openpyxl-3.0.7 
pyyaml-5.4.1 
six-1.16.0 
tablib-3.0.0 
xadmin-0.6.1 xlrd-2.0.1
xlwt-1.3.0

二,注册 xadmin:

'xadmin',
'crispy_forms',
'reversion',

crispy_forms与reversion是xadmin的依赖包,使用上面的命令会自动安装。

三,配置 URL:

其余代码

from xadmin.plugins import xversion

import xadmin
xversion.register_models()
xadmin.autodiscover()

urlpatterns = [
    其余代码
    path('xadmin/', xadmin.site.urls),
]

四,数据迁移并创建超级用户

五,收集media

python manage.py collectstatic

六,运行项目,解决错误

运行项目,出现下面的报错:

1,ImportError: cannot import name ‘six’ from ‘django.utils’

在 site-packages 目录中找到 six.py 文件,将它复制到 django/utils 目录下。

运行项目,出现下面的报错。

2,No module named ‘django.core.urlresolvers’

有博客说是:

因为Django 3.x 已经把django.core.urlresolvers模块的内容迁移至django.urls中

将:

from django.core.urlresolvers import reverse
改为:
from django.urls import reverse

这个错误多次出现,直接按上面的方法处理。

运行项目,出现下面的报错。

3,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

运行项目,出现下面的报错。

4,ImportError: cannot import name ‘pretty_name’ from ‘django.forms.forms’

将:
from django.forms.forms import pretty_name
改为:
from django.utils import formats, six

运行项目,出现下面的报错。

5,ModuleNotFoundError: No module named ‘django.contrib.staticfiles.templatetags’

将:
from django.contrib.staticfiles.templatetags.staticfiles import static
改为:
from django.templatetags.static import static

运行项目,出现下面的报错。

6,TypeError: init() missing 1 required positional argument: ‘on_delete’

这个错误多次出现,直接在字段中添加:

on_delete=models.CASCADE

运行项目,出现下面的报错。

7,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)

运行项目,出现下面的报错。

8,ImportError: cannot import name ‘login’ from ‘django.contrib.auth.views’

将:
from django.contrib.auth.views import login
改为:
from django.contrib.auth.views import LoginView as login
将:
from django.contrib.auth.views import logout
改为:
from django.contrib.auth.views import LogoutView as logout

随之而来的同文件下的改动:

注释掉
class LoginView(BaseAdminView) 与 class LogoutView(BaseAdminView) 的 get 方法中 defaults 字典的 'current_app': self.admin_site.name,

修改
class LoginView(BaseAdminView) 与 class LogoutView(BaseAdminView) 的 get 方法中 return 为 return login.as_view(**defaults)(request) 与 return logout.as_view(**defaults)(request)

运行项目,出现下面的报错。

9,ImportError: cannot import name ‘FieldDoesNotExist’ from ‘django.db.models.fields’

先安装 django-advanced-filters,
再:

将:
from django.db.models.fields import FieldDoesNotExist
改为:
from django.core.exceptions import FieldDoesNotExist

运行项目,出现下面的报错。

10,ImportError: cannot import name ‘QUERY_TERMS’ from ‘django.db.models.sql.query’

将:
from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS
改为:
from django.db.models.sql.query import LOOKUP_SEP, Query

同文件下,将:

if len(parts) > 1 and parts[-1] in QUERY_TERMS:
改为:
if len(parts) > 1 and parts[-1] in Query:    

运行项目,出现下面的报错。

11,ModuleNotFoundError: No module named ‘requests’

安装requests

运行项目,出现下面的报错。

12,ImportError: cannot import name ‘FieldDoesNotExist’ from ‘django.db.models’

将:
from django.db.models import FieldDoesNotExist, Avg, Max, Min, Count, Sum
改为:
from django.core.exceptions import FieldDoesNotExist
from django.db.models import Avg, Max, Min, Count, Sum

运行项目,出现下面的报错。

13,ImportError: cannot import name ‘password_reset_confirm’ from ‘django.contrib.auth.views’

将:
from django.contrib.auth.views import password_reset_confirm
改为:
from django.contrib.auth.views import PasswordResetConfirmView

运行项目,出现下面的报错。

14,AttributeError: ‘Settings’ object has no attribute ‘MIDDLEWARE_CLASSES’

在 language.py 中修改:

将:
if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE_CLASSES:
改为:
if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE:

运行项目,出现下面的报错。

15,ImportError: cannot import name ‘SKIP_ADMIN_LOG’ from ‘import_export.admin’

将:
from import_export.admin import DEFAULT_FORMATS, SKIP_ADMIN_LOG, TMP_STORAGE_CLASS
改为:
from import_export.admin import DEFAULT_FORMATS,ImportMixin,ImportExportMixinBase

七,访问xadmin

创建超级用户后,运行项目,访问http://127.0.0.1:8000/xadmin/,

16,AttributeError: ‘Media’ object has no attribute ‘add_css’

将:
def vendor(*tags):
改为:
def vendor(*tags):
    media = Media()
    css = {'screen':[]}
    js = []
    for tag in tags:
        file_type = tag.split('.')[-1]
        files = xstatic(tag)
        if file_type == 'js':
            # media.add_js(files)
            js.extend(files)
        elif file_type == 'css':
            # media.add_css({'screen': files})
            css['screen'] += files
    # return media
    return Media(css=css, js=js)

运行项目,出现下面的报错。

17,ImportError: cannot import name ‘javascript_catalog’ from ‘django.views.i18n’

将:
    def i18n_javascript(self, request):
        if settings.USE_I18N:
            from django.views.i18n import javascript_catalog
        else:
            from django.views.i18n import null_javascript_catalog as javascript_catalog
        return javascript_catalog(request, packages=['django.conf', 'xadmin'])
改为:
    def i18n_javascript(self, request):
        # if settings.USE_I18N:
        #     from django.views.i18n import javascript_catalog
        # else:
        #     from django.views.i18n import null_javascript_catalog as javascript_catalog
        from django.views.i18n import JavaScriptCatalog
        # return javascript_catalog(request, packages=['django.conf', 'xadmin'])
        return JavaScriptCatalog.as_view(packages=['django.conf', 'xadmin'])(request)

访问http://127.0.0.1:8000/xadmin/。

点击左侧 Revisions 出现下面的报错。

18,AttributeError: ‘DateTimeField’ object has no attribute ‘rel’

将:
if isinstance(field.rel, models.ManyToOneRel):
改为:
if isinstance(field.remote_field, models.ManyToOneRel):

这个错误会在第一次点击左边的所有导航内容时出现,只需将 rel 改为 remote_field 就行。

点击主页的“添加小组件”按钮时,出现下面报错。

19,TypeError: render() got an unexpected keyword argument ‘renderer’

将 xadmin\views\dashboard.py WidgetTypeSelect类中:
def render(self, name, value, attrs=None):
改为:
def render(self, name, value, attrs=None, renderer=None):

或者

将/lib/site-packages/django/forms/boundfield.py中as_widget的return的最后一个参数注释掉。

OK! 暂时到这里吧,后面遇到问题再在下面补充。

之所以在 django3.2.1 中使用 xadmin 会出现这么多报错,是因为 xadmin 针对 django 进行开发的版本最多到 djang 2,后面 xadmin 成为一个完全的前端框架,对 django 的专门支持不如以前了。

八,问题补充

20,‘ManyToOneRel’ object has no attribute ‘to’

将:
self.add_url, (_('Create New %s') % self.rel.to._meta.verbose_name), name,
改为:
self.add_url, (_('Create New %s') % self.rel), name,

21,‘Media’ object has no attribute ‘add_js’

将xadmin/util.py的:
def vendor(*tags):
    media = Media()
    for tag in tags:
        file_type = tag.split('.')[-1]
        files = xstatic(tag)
        if file_type == 'js':
            media.add_js(files)
        elif file_type == 'css':
            media.add_css({'screen': files})
    return media

改为:
def vendor(*tags):
    css = {'screen': []}
    js = []
    for tag in tags:
        file_type = tag.split('.')[-1]
        files = xstatic(tag)
        if file_type == 'js':
            js.extend(files)
        elif file_type == 'css':
            css['screen'] += files
    return Media(css=css, js=js)

22,AttributeError: module ‘django.db.models’ has no attribute ‘FieldDoesNotExist’

报错文件:venv/lib/python3.8/site-packages/xadmin/views/list.py:

我暂时无法解决!!

23,AttributeError: ‘ForeignKey’ object has no attribute ‘rel’

参考问题 18。

24,AttributeError: ‘CharField’ object has no attribute ‘rel’

参考问题 18。

25,AttributeError: ‘ManyToManyField’ object has no attribute ‘rel’

参考问题 18。

26,Exception: Relate Lookup field must a related field

将 xadmin\util.py的:

return (hasattr(field, 'rel') and field.rel != None) or is_related_field(field)
修改为:
return (hasattr(field, 'remote_field') and field.remote_field != None) or is_related_field(field)

27,AttributeError: ‘set’ object has no attribute ‘get_field_names’

28,TypeError: get_deleted_objects() takes 3 positional arguments but 5 were given

将:
deletable_objects, model_count, perms_needed, protected = get_deleted_objects(
            queryset, self.opts, self.user, self.admin_site, using)
改成:
deletable_objects, model_count, perms_needed, protected = get_deleted_objects(
            queryset, self.user, self.admin_site)

29,AttributeError at /xadmin/xadmin/userwidget/4/delete/ type object ‘xadminuserwidgetAdmin’ has no attribute ‘has_delete_permission’

这个问题在我删除已存在的”用户小组件“时出现。

直接注释掉报错处:

# if not admin_site._registry[model].has_delete_permission(request, obj):
#    perms_needed.add(opts.verbose_name)

30,IndexError: list index out of range

将:
input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).replace('/><input', '/>\n<input').split('\n') if ht != '']
改为:
input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).split('/><') if ht != '']
input_html[0] = input_html[0] + "/>"
input_html[1] = "<" + input_html[1]

参考:

Django3.1.7使用Xadmin(只为记录坑)
Django 3.x 版本使用xadmin问题汇总
Django 由 admin 转 xadmin 填坑
Django2集成xadmin详解-4-list index out of range报错追踪和处理

建议还是别用django-xadmin了,Simple UI、grappelli 它们不更香吗?

加载全部内容

相关教程
猜你喜欢
用户评论