xadmin在Django 1.11中的使用及中英文切换

版权声明:本文为博主原创文章,欢迎转载,并请注明出处。联系方式:460356155@qq.com

xadmin是一个强大的替代django admin的管理后台,github地址为:https://github.com/sshwsfc/xadmin,为了匹配django最新的1.11版本,建立了1.11版本支持的分支:https://github.com/sshwsfc/xadmin/tree/django1.11

本文简单介绍下xadmin在django 1.11版中的使用及注意事项。

一、django基本使用

1、django-admin startproject xxx

2、python manage.py runserver,这时可访问页面http://127.0.0.1:8000/,出来:

It worked!

Congratulations on your first Django-powered page.

3、此时无法访问admin后台,继续:

4、python manage.py migrate,python manage.py createsuperuser,python manage.py runserver,可访问admin:

Site administration

二、xadmin在django 1.11的使用

1、在github分支上下载xadmin-django1.11.zip,解压

2、拷贝xadmin到项目目录下(和manage.py在同一级目录)

3、在settings.py的INSTALLED_APPS中添加:

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

在MIDDLEWARE添加:

'django.middleware.locale.LocaleMiddleware',

在TEMPLATES添加:

'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',

另外添加行:

from django.utils.translation import ugettext_lazy as _

LANGUAGES = (
    ('en', _('English')),
    ('zh-hans', _('Chinese')),
)

4、在urls.py添加行:

from django.conf.urls import include
import xadmin

xadmin.autodiscover()

from xadmin.plugins import xversion
xversion.register_models()

在urlpatterns添加:

url(r'xadmin/', include(xadmin.site.urls)),    

5、执行:python manage.py migrate,python manage.py runserver,可访问xadmin:http://127.0.0.1:8000/xadmin/,出来:

三、xadmin在django 1.11使用中的中英文切换问题

通过前面的步骤,xadmin可正常使用,但却没有xadmin demo中显示的中英文切换功能。原因在xadmin\plugins\language.py文件中:

if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE_CLASSES:
    site.register_plugin(SetLangNavPlugin, CommAdminView)
    site.register_view(r'^i18n/setlang/$', SetLangView, 'set_language')

django从1.10开始MIDDLEWARE_CLASSES更名为MIDDLEWARE,因此上面if中的代码未执行,因此也就没有中英文切换功能。

解决办法有几种:

1、把settings.py的MIDDLEWARE修改为MIDDLEWARE_CLASSES。

2、language.py中的MIDDLEWARE_CLASSES改为MIDDLEWARE。

3、language.py中的if语句改为:

if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in (settings.MIDDLEWARE_CLASSES + settings.MIDDLEWARE):
posted @ 2017-07-29 14:39  碧水青山  阅读(1379)  评论(0编辑  收藏  举报