SoFunction
Updated on 2024-12-20

Django-xadmin backend import json data and backend display information icon and theme change way

Django comes with a powerful day after day management system, next I will give you an introduction to x's admin some powerful operations and background beautification.

First of all to introduce you to some xadmin registration creation, first download the official xadmin plugin package into the corresponding third-party apps_extra folder

The next step is to register xadmin and crisp_forms to INSTALLED_APPS in the SETTING, and the next step is to configure routing in our file

import xadmin
 
urlpatterns = [
"""
  url(r'^xadmin/', )
"""
]

The next step is to create the super administrator account by typing the command in the Operations Console, the command is python createsupperuser

Follow the prompts to enter the appropriate account, email and username, remember to log in to the xadmin backend before you must Migrate synchronization, xadmin corresponding tables to mysql database

The next step is to run our project and add /xadmin to the end of the corresponding url to log in to our admin backend.

The next step is to create a new adminx file in each app and register the response mods into xadmin

import xadmin
from .models import Goods
class GoodCategoryBrandAdmin(object):
  fields=['category','name','image','desc','add_time']
# Configure the order of the fields on the detail page and whether to display them or not, if you don't write it, all fields will be displayed by default.
  list_display=['name','desc','add_time']
# Configuration of list display fields
  search_fileds=['name']
# Search box search content configuration
  list_per_page=10
# Configuration of list display paging, how much data to display per page
  list_filter=['name']
# Filter Configuration
  
(Goods,GoodCategoryBrandAdmin)
#commander-in-chief (military)GoodsRegister toxadminthe area behind a theatrical stage

The next step is to register each of the app's mods into the xadmin backend, the following picture shows the effect after I've finished registering all of them

The name of the starting background is django_xadmin, you must want to replace the corresponding name of their own background, the next step is to replace the name of the background and labeling and navigation bar expansion effect of the

from xadmin import views
class GlobalSettings(object):
  site_title = "Silicon Valley Mall Back Office Management System"
  # System name
  site_footer = "Copyright @ Silicon Valley Mall."
  # Copyright field at the bottom
  menu_style = "accordion"
  # Put the menu bar away
(,GlobalSettings)

Write this code to any file in the app and the effect will be displayed

In django-xadmin also supports the replacement of the background theme, the settings are also very simple, if we want to replace the background of the gray and black into blue or green, etc.

It's just a matter of doing the next few steps.

class BaseSetting(object):
Setting the theme function
  enable_themes = True
  use_bootswatch = True
 
(, BaseSetting)

Next we can modify the theme of the xadmin background, in the initial function module is a small circle by default, we can set the function module icon we want through the font-awesome, the following picture is the completion of my setup of the function module icons

Let's start by finding the code that sets the image

model_icon = 'fa fa-picture-o'

This code for registering the backend icon is in, as shown here

The next step is to find our favorite icons through the / website, here is a very full range of icons, here is a screenshot of some of the official website icons

If we want to be able to add the latest version of the icon, we download the latest version from the official website, copy or replace the css and font in the downloaded file to the red folder marked in the following figure

The next step is to replace the content in model_icon = 'fa fa-picture-o' with the css style marked in red in the following image

model_icon = 'fa fa-address-card' added to the corresponding class in relativity

class GoodCategoryBrandAdmin(object):
"""
  model_icon = 'fa fa-address-card' 
"""

After the completion of the setup we will be able to see the corresponding model icon in the background can appear, if we background data import data provided by a third party or some of the data written in the json format of the test, we can import the data in the following way as a whole, first of all, the original data will be copied to the corresponding folder

Next, create the import_category.py file in the same directory as data, and write the function in the py file

import os
import sys
#Import packages
filename=(__file__)
# The name of the corresponding file
dirname=(filename)
# The name of the corresponding folder
(0,dirname)
#Insert the folder
("DJANGO_SETTINGS_MODULE", "")
import django
()
from db_utils.data.category_data import row_data
# Import raw data
from  import GoodsCategory
# Import classes in models
for item in row_data:
# Facilitating data
  instanc1=GoodsCategory()
# Instantiate the class
  =item['name']
# Add the traversed data to the fields in the models
  =item['code']
  instanc1.category_type=1
# Labeled categories are first level of directory
  ()
# Data saved to database
 
  for item2 in item['sub_categorys']:
    instanc2 = GoodsCategory()
     = item2['name']
     = item2['code']
    # Point to parent directory
    instanc2.parent_category=instanc1
    instanc2.category_type=2
    ()
    for item3 in item2['sub_categorys']:
      instanc3 = GoodsCategory()
       = item3['name']
       = item3['code']
      instanc3.parent_category = instanc2
      instanc3.category_type=3
      ()

Next we run import_category file, note that can only be run once, if many times run, will be many times to add the database. Next, we will check the database and background in the data we imported, the next import_product and the above figure is similar to the operation, the following is the code section

import os
import sys
filename=(__file__)
dirname=(filename)
(0,dirname)
("DJANGO_SETTINGS_MODULE", "")
import django
()
from db_utils.data.product_data import row_data
from  import Goods,GoodsCategory,GoodsImage,GoodCategoryBrand
for item in row_data:
  goods=Goods()
  goods.market_price=float(item['market_price'].replace('Yuan','').replace('¥',''))
  goods.shop_price=float(item['sale_price'].replace('Yuan','').replace('¥',''))
  =item['name'] if item['name'] is not None else ''
  goods.goods_desc=item['goods_desc'] if item['goods_desc'] is not None else ''
  goods.goods_brief=item['desc'] if item['desc'] is not None else ''
  category_name=item['categorys'][-1]
  categorys=(name=category_name)
  if categorys:
    =categorys[0]
  ()
  for image in item['images']:
    goods_image=GoodsImage()
    goods_image.image=image
    goods_image.goods=goods
    goods_image.save()

Supplementary imported files are pictures, if you want to let the background of the picture is displayed, but also need the next configuration, first of all need to be configured in the setting as shown in Figure

MEDIA_URL='/media/'
# Configure xadmin to display images
MEDIA_ROOT=(BASE_DIR,'media')
#foldersmediaAdded to the system

Then, configure the image url in the url, after these two configurations are completed the image will be able to display it

from  import MEDIA_ROOT
urlpatterns = [
  
  url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT})
  #Image Path
 
]

Above is the xadmin background part of the operation, django comes with the xadmin background is very powerful, very easy to use. Next, I will continue to add some knowledge of django's operations.

Above this Django-xadmin backend import json data and backend display information icons and theme change the way is all I share with you, I hope to be able to give you a reference, and I hope you will support me more.