Installation and Configuration ############### ############### xadmin
"" " Environment: Python3.6.3 django1.11.11 create django project first project where you want to create a directory, then enter the directory, execute the command: django-ADMIN startproject test1 # (project name) Source Installation 1, from https :. //github.com/sshwsfc/xadmin xadmin ZIP file download, then extract 2, App under item, such as the user extra_app new folder, the file is copied to the extracted folders xadmin extra_app in 3, and then the file right-click on the folder extra_app select 'Mark Directory as Sources Root'. 've created extra_apps, need to configure the settings in extra_apps set to the path searchable.. Import os Import SYS # Inside at the Project Build paths like the this: os.path. the Join (base_dir, ...) base_dir = os.path.dirname (os.path.dirname (os.path.abspath with (__ file__))) sys.path.insert (0, the os.path.join (base_dir, 'Apps ')) sys.path.insert (0, os.path.join (BASE_DIR, 'extra_apps')) # EXTRA_APPS folder to the directory search and add the following items are arranged in settings.py = TIME_ZONE 'Asia / on Shanghai' USE_TZ = False # If there is no demand for international writing False, otherwise the data inserted in time, there is a warning. = The INSTALLED_APPS [ ..... 'xadmin', 'crispy_forms', ' reversion ', ] if you are using a MySql database, as follows DATABASES = { ' default ': { ' ENGINE ':' django.db.backends .mysql ', ' NAME ':' mydb ', ' the USER ':' the root ', ' PASSWORD ':' 123456 ', ' the HOST ':' 139.107.172.158 ', ' PORT ':' 3306 ', } } while into the Chinese language, time zone changed to Shanghai LANGUAGE_CODE = 'zh-Hans' database migration: Next configured to route open project urls.py modified as follows Import xadmin the urlpatterns = [ ... URL (R & lt 'xadmin ^ /', xadmin.site.urls), ] to generate python manage.py makemigrations migration the migration python manage .py migrate to this step actually can run up a python mange.py runserver then visit http://127.0.0.1:8000/xadmin/, should be the login screen "" "
############### ############### model management
# Adminx.py class CashTitleContentAdmin (Object): # icon menu model_icon = ' FA FA-Image ' # list displays contents list_display = ( ' SUBTITLE ' , ' content_cash ' ,) # list_display_links default settings editable field list_display_links = ( ' SUBTITLE ' ,) # # page display # list_per_page = settings.list_per_page # filter list_filter = ( ' content_cash ' ,) #Form display contents Fields = ( ' SUBTITLE ' , ' content_cash ' ,) # search field search_fields = ( ' SUBTITLE ' , ' content_cash__title ' ,) xadmin.site.register (cash_title_content, CashTitleContentAdmin)
Modify ############### ############### app name
# App apps.py named under the Users from django.apps Import AppConfig class UsersConfig (AppConfig): # Set app icon app_icon = ' FA FA-Line-Chart ' # app name name = ' the Users ' verbose_name = U ' User Management ' # __init__.py default_app_config = ' users.apps.UsersConfig '
############### ############### Global Configuration and Basic Configuration
class BaseSetting (Object): "" " basic configuration of xadmin " "" enable_themes = True # open theme switching function use_bootswatch = True # support switch themes xadmin.site.register (views.BaseAdminView, BaseSetting) class GlobalSettings (Object): SITE_TITLE = " coordination office platform back office systems " site_footer = " Copyright © 2019-2021 xxx technology. Version1.0.0 " menu_style = " Accordion " # navigation menu is folded xadmin.site.register (views.CommAdminView, GlobalSettings)
Invalid theme modification solution:
Here, instead of using the library requests httplib2 modifications xadmin source directory xadmin\plugins\themes.py
:
#coding:utf-8 from __future__ import print_function import httplib2 from django.template import loader from django.core.cache import cache from django.utils import six from django.utils.translation import ugettext as _ from xadmin.sites import site from xadmin.models import UserSettings from xadmin.views import BaseAdminPlugin, BaseAdminView from xadmin.util import static, json import six if six.PY2: import urllib else: import urllib.parse import requests THEME_CACHE_KEY = 'xadmin_themes' class ThemePlugin(BaseAdminPlugin): enable_themes = False # {'name': 'Blank Theme', 'description': '...', 'css': 'http://...', 'thumbnail': '...'} user_themes = None use_bootswatch = False default_theme = static('xadmin/css/themes/bootstrap-xadmin.css') bootstrap2_theme = static('xadmin/css/themes/bootstrap-theme.css') def init_request(self, *args, **kwargs): return self.enable_themes def _get_theme(self): if self.user: try: return UserSettings.objects.get(user=self.user, key="site-theme").value except Exception: pass if '_theme' in self.request.COOKIES: if six.PY2: func = urllib.unquote else: func = urllib.parse.unquote return func(self.request.COOKIES['_theme']) return self.default_theme def get_context(self, context): context['site_theme'] = self._get_theme() return context # Media def get_media(self, media): return media + self.vendor('jquery-ui-effect.js', 'xadmin.plugin.themes.js') # Block Views def block_top_navmenu(self, context, nodes): themes = [ {'name': _(u"Default"), 'description': _(u"Default bootstrap theme"), 'css': self.default_theme}, {'name': _(u"Bootstrap2"), 'description': _ (In"Bootstrap 2.x theme"), 'css': self.bootstrap2_theme}, ] select_css = context.get('site_theme', self.default_theme) if self.user_themes: themes.extend(self.user_themes) if self.use_bootswatch: ex_themes = cache.get(THEME_CACHE_KEY) if ex_themes: themes.extend(json.loads(ex_themes)) else: ex_themes = [] try: # h = httplib2.Http() # resp, content = h.request("https://bootswatch.com/api/3.json", 'GET', '', # headers={"Accept": "application/json", "User-Agent": self.request.META['HTTP_USER_AGENT']}) # if six.PY3: # content = content.decode() # watch_themes = json.loads(content)['themes'] # ex_themes.extend([ # {'name': t['name'], 'description': t['description'], # 'css': t['cssMin'], 'thumbnail': t['thumbnail']} In Flag:IFif their code is True, if access is Flase, requests library using# In Flag = False For T in watch_themes])# H = the httplib2.Http () RESP, Content = h.request ( " http://bootswatch.com/api/3.json " , ' the GET ' , ' ' , headers = { " the Accept " : " file application / JSON " , " the User-- Agent " : self.request.META [ ' the HTTP_USER_AGENT ']}) if six.PY3: content = content.decode() watch_themes = json.loads(content)['themes'] else: content = requests.get("https://bootswatch.com/api/3.json") if six.PY3: content = content.text.decode() watch_themes = json.loads(content.text)['themes'] ex_themes.extend([ {'name': t['name'], 'description': t['description'], 'css': t['cssMin'], 'thumbnail': t['thumbnail']} for t in watch_themes]) except Exception as e: print(e) cache.set(THEME_CACHE_KEY, json.dumps(ex_themes), 24 * 3600) themes.extend(ex_themes) nodes.append(loader.render_to_string('xadmin/blocks/comm.top.theme.html', {'themes': themes, 'select_css': select_css})) site.register_plugin(ThemePlugin, BaseAdminView)
############### xadmin in the Model category management (proxy = True) ###############
# Adminx.py Import xadmin from .models Import Teacher, TeacherMan class TeacherAdmin (Object): # field displays list_display = [ " TEACHER_NAME " , " Sex " , " Tel " , " mail " ] # registration table new class TeacherManAdmin ( TeacherAdmin): # field displays list_display = [ " TEACHER_NAME " , " Sex " , "tel", "mail"] def queryset(self): qs = super(TeacherAdmin, self).queryset() qs = qs.filter(sex="M") # 筛选 sex="男" return qs xadmin.site.register(Teacher, TeacherAdmin) xadmin.site.register(TeacherMan, TeacherManAdmin)
--Queryset ############### ############### filter data according to a user or group logged
class DeviceAdmin (Object): ... DEF QuerySet (Self): "" " function role: the currently logged on user equipment can only see their own responsibility " "" QS = Super (DeviceAdmin, Self) .queryset () IF self.request.user.is_superuser: return QS return qs.filter (area_company = Group.objects.get (= User self.request.user))
Installation and Configuration ############### ############### xadmin
Installation and Configuration ############### ############### xadmin
Installation and Configuration ############### ############### xadmin