Introduction of xadmin
As everyone knows, Djangos admin background management interface is not very pretty, and the usage habits do not conform to domestic customs. Therefore, we will choose a background xadmin based on Bootstrap, which is more suitable for domestic usage and has a better interface.
Xadmin downloads the decompression configuration
-
Download link: github.com/sshwsfc/xad… (Note: download the xadmin zip package.)
-
Unzip to the local directory, unzip after an Xadmin folder, is we want to use the xadmin source code
Integrate Xadmin into our project
-
Project preparation
Create a New Django project and skip it if you integrate it with an existing project
-
Create a new folder extra_apps in the project root directory
Copy the previously unzipped xadmin folder to the extra_apps directory
-
Select the EXTRA_Apps folder by pycharm and right-click the menu mark Directory as to select Sources Root
-
Modify settings.py configuration
# Add the following apps to INSTALLED_APPS
INSTALLED_APPS = [
......
'xadmin'
'crispy_forms'
'reversion'
]
# Add the extra_apps directory
sys.path.insert(0,os.path.join(BASE_DIR,'extra_apps'))
Copy the code
- Configure urls.py to add xadmin routes
import xadmin
urlpatterns = [
#path('admin/', admin.site.urls), # Django comes with admin
path('admin/', xadmin.site.urls),
]
Copy the code
- Migrating data
The following two commands are executed on the command line, as well as on the existing project, to generate the xadmin related table
python manage.py makemigrations
python manage.py migrate
Copy the code
Here our xadmin has inherited our Django project, run the project, the browser visit: http://127.0.0.1:8000/admin/ routing can not only see the login screen
Write admin.py module
Djangos built-in admin registration is a little different from xadmin. We’ll register our model with Xadmin in the admin.py module below
import xadmin
from .models import Orders
class OrderAdmin(object):
# xadmin display field
list_display = ['orderid'.'mobile'.'add_time']
# Introduce a search box, which can be searched by image or URL
search_fields = ['order_id'.'mobile']
# add filter
list_filter = ['add_time']
# registered
xadmin.site.register(Orders, OredrAdmin)
Copy the code
After registration, refresh the xadmin background to see our registered model
** Common parameter meaning of the xadmin module
Search_fields controls the name of the field that can be searched by the search box. Xadmin uses the fuzzy query list_filter column that can be filtered out readonly_fields read-only field and exclude list_editable field that can be hidden on the editing page The show_detail_FILeds field that can be quickly and directly edited on the list page displays detailed information refresh_times Specifies the periodic refresh of the list page. List_export Controls the optional format show_bookmarks for exporting data on the list page Control whether to display bookmarks function data_charts control display icon style model_icon Control menu iconCopy the code
4. Xadmin extends other functions
- Change the default top menu and bottom menu to display content and left menu to fold
Add the following code to admin.py
from xadmin import views
class GlobalSetting(object):
# set the background top title
site_title ='XXX Management background'
# Set the background bottom title
site_footer ='XXX company'
# Set left menu folding
menu_style = "accordion"
# register to background
xadmin.site.register(views.CommAdminView, GlobalSetting)
Copy the code
Restart the program to see what we have changed
# Add code to admin.py
class BaseSetting(object):
# Enable theme manager
enable_themes =True
# Use themes
use_bootswatch =True
# Register theme Settings
xadmin.site.register(views.BaseAdminView, BaseSetting)
Copy the code
Restart the program and you will see the button to switch themes
5. Common problems
Q: Chinese is not displayed in the xadmin background
Solution:
LANGUAGE_CODE =' zh-hans', TIME_ZONE ='Asia/Shanghai'Copy the code
After saving, refresh the interface and you can see that Chinese has been displayed.
Q: The self-registered model displays the table name
Solution:
Customize the background menu when creating the Models model
class Order(models.Model):
order_id = models.CharField(max_length=64, null=False, verbose_name="Order Number")
mobile = models.CharField(max_length=11, null=False, verbose_name="Pick up hand number")
class Meta:
db_table ='Order'
# Define the background menu display content
verbose_name ="Students"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
Verbose_name specifies the name of the field you want to display
Define menu names in Meta functions
Copy the code
Continuously updated…
~~ Over ~~~~Copy the code