xadmin installation

xadmin installation

Environment (must be the same)

  • Python 3.6.2
  • Django 2.0

installation

  1. pip install django==2.0, Specifying a particular version
  2. pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2Download django2 branch from the official github packages can also be downloaded through the pack down and then use pip to install, but is not recommended for use pip install, it is recommended xadmin into the libs
  3. Directory Structure

.
├── __pycache__
│   └── manage.cpython-36.pyc
├── apps
│   ├── __pycache__
│   ├── food
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-36.pyc
│   │   │   ├── admin.cpython-36.pyc
│   │   │   ├── adminx.cpython-36.pyc
│   │   │   ├── apps.cpython-36.pyc
│   │   │   └── models.cpython-36.pyc
│   │   ├── admin.py
│   │   ├── adminx.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-36.pyc
│   │   │       └── __init__.cpython-36.pyc
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   └── user
│       ├── __init__.py
│       ├── __pycache__
│       │   ├── __init__.cpython-36.pyc
│       │   ├── admin.cpython-36.pyc
│       │   ├── adminx.cpython-36.pyc
│       │   ├── apps.cpython-36.pyc
│       │   └── models.cpython-36.pyc
│       ├── admin.py
│       ├── adminx.py
│       ├── apps.py
│       ├── migrations
│       │   ├── 0001_initial.py
│       │   ├── 0002_auto_20190621_1320.py
│       │   ├── __init__.py
│       │   └── __pycache__
│       │       ├── 0001_initial.cpython-36.pyc
│       │       ├── 0002_auto_20190621_1320.cpython-36.pyc
│       │       └── __init__.cpython-36.pyc
│       ├── models.py
│       ├── tests.py
│       └── views.py
├── libs
├── manage.py
├── media
├── static
├── templates
├── utils
│   └── __init__.py
└── xadmintest
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-36.pyc
    │   ├── settings.cpython-36.pyc
    │   ├── urls.cpython-36.pyc
    │   └── wsgi.cpython-36.pyc
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Configuration

  1. All app in apps directory, libs copied third-party storage library, you need to add the following in settings.py, the INSTALL_APPStime can be written directly app name (written recommendation, do not write may be wrong)

sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
sys.path.insert(0, os.path.join(BASE_DIR, 'libs'))
  1. In the settings.py INSTALL_APPSadd xadmin, reversion,crispy_forms
  2. Settings.py in the language set to support Chinese

    
    LANGUAGE_CODE = 'zh-hans'
    TIME_ZONE = 'Asia/Shanghai'
    USE_I18N = True
    USE_L10N = True
    USE_TZ = False # 设置为 False 则使用本地时间,否则会使用国际时间
  3. If you are using django comes with a user module, but also to add in the settings.py AUTH_USER_MODEL = 'user.UserProfile'for User overloaded system , in which the user is the app name, UserProfile is the successor of the app under models.py file defined in the django.contrib.auth.model.AbstractUserclass name of the class
  4. The url.py the url changed path('xadmin/', xadmin.site.urls)
  5. Configured for each model a admin, but is no longer used admin.py use adminx.py, in adminx.py in, and almost admin.py usage, we only need to provide list_display, search_fields,list_filter


class UserAdmin(object):

    list_display = ['name', 'birthday', 'gender']
    search_fields = ['name', 'birthday', 'gender']
    list_filter = ['name', 'birthday', 'gender']

# xadmin.site.register(UserProfile, UserAdmin), model 不能为 User, 因为内部已经使用了
# 注意, 在扩展 Django 自带的 User 模块时, 需要设置 null=True, blank=True, 否则在之后的 ./manage.py createsuperuser 中会失败
  1. xadmin Some global configuration, generally defined in the succession of models AbstractUser

class BaseSetting(object):

    enable_themes = True
    use_bootswatch = True


class GlobalSettings(object):

    site_title = '我的管理后台'
    site_footer = '我的后台'

xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)
  1. Configuring background app to display Chinese, in each app's __init__.pyadded

default_app_config = 'user.apps.UserConfig' # 这里的 user 为 app 名, UserConfig 为 apps.py 中的类的类名, 要在 UserConfig 中填写 name 和 verbose_name 两个属性, 其中 verbose_name 为中文
  1. Database migration

./manage.py makemigrations && ./manage.py migrate
  1. Create an administrator user

./manage.py createsuperuser
  1. If you want to use the rich text editor in the model, you need to copy DjangoUeditor (Django project is Baidu) into the libs, add ueditor.py plug-in xadmin of plugins, the content below, which is from a person's blog derek in resulting in the model, if you want to use rich-text field, you need to import UEditor in UEditorField, also use the following

# libs/xadmin/plugins/ueditor.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import xadmin
from xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminView
from DjangoUeditor.models import UEditorField
from DjangoUeditor.widgets import UEditorWidget
from django.conf import settings


class XadminUEditorWidget(UEditorWidget):

    def __init__(self, **kwargs):
        self.ueditor_options = kwargs
        self.Media.js = None
        super(XadminUEditorWidget,self).__init__(kwargs)


class UeditorPlugin(BaseAdminPlugin):

    def get_field_style(self, attrs, db_field, style, **kwargs):
        if style == 'ueditor':
            if isinstance(db_field, UEditorField):
                widget = db_field.formfield().widget
                param = {}
                param.update(widget.ueditor_settings)
                param.update(widget.attrs)
                return {'widget':XadminUEditorWidget(**param)}
        return attrs

    def block_extrahead(self, context, nodes):
        print(settings.STATIC_URL)
        js = '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.config.js")
        js += '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.all.min.js")
        nodes.append(js)


xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)
xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)


# apps/yourmodel/models.py

class Food(models.Model):

    name = models.CharField(max_length=30, verbose_name='食品名称')
    price = models.FloatField(default=0, verbose_name='价格')
    brief = models.TextField(max_length=500, verbose_name='简介')
    description = UEditorField(verbose_name='内容', imagePath='food/images/', filePath='food/files/', default='')
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)

    class Meta:
        verbose_name = '食品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

Guess you like

Origin www.cnblogs.com/megachen/p/11074716.html