Django builds a personal blog platform 4---Background admin optimization, simpleui and import and export

Article Directory

Django builds personal blog platform 4-background admin optimization, simpleui and import and export

Admin optimization

The display and search buttons can be modified directly in ModelAdmin.

Display field settings

I want to display a few more fields on the UserInfo table, not just the user name.

First, customize a management class, inherit admin.ModelAdmin, and then change its list_display. Finally, don't forget to add your management class when registering.

from django.contrib import admin


class UserManager(admin.ModelAdmin):
    list_display = ['username', 'email', 'is_active', 'last_login']
    
    
admin.site.register(models.UserInfo, UserManager)

Add filter

Use admin to register the decorator. When the display field is a foreign key, we use the function to handle it .

def category(obj):
    return obj.category.name


@admin.register(models.Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ['title', category, 'update_time']  # 可以是一个函数
    list_filter = ['title']  # 过滤器
    form = ArticleForm
class LinkManager(admin.ModelAdmin):
    list_display = ['title', 'desc']
    list_display_links = ['title', 'desc']   # 给那个字段加url


admin.site.register(models.Links, LinkManager)

Editable field

Note: You cannot add editable to the fields in links

class LinkManager(admin.ModelAdmin):
    list_display = ['title', 'desc', 'is_disply']
    list_editable = ['is_disply']  # 可编辑的字段
    list_display_links = ['title', 'desc']   # 给那个字段加url

simpleui

Basic use

Its admin is too ugly, what to do if it doesn't look good. You can quickly optimize with a plug-in.

Install simpleui

pip install django-simpleui==2021.4.3

Register in the app

Be sure to write it in the first one , and then register your own applications here.

# settings.py

INSTALLED_APPS = [
    'simpleui',  # 一定要写在第一个
    'import_export',
    'smart_chart.echart',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.sitemaps',
    'blog',
    'mdeditor',
]

This is done, go to the background to check that it has changed, and it is more beautiful.

Advanced

Rewrite the template html file of simple-ui

I want to remove the simple-ui version and link display on the right, and I need to rewrite its home page

First of all, comparing the directory structure of simple-ui, I also created the same directory structure in the template folder under my project

That is, create an admin folder under the templates folder of my project, and then create an html file with the same name like rewriting which html.

I saw that the homepage is home.html, there are not many hooks reserved in it, so I just copy it all over and delete the unwanted ones.

image-20210517135519722

Another way is to modify it directly in the configuration file:

# simpleui配置
SIMPLEUI_HOME_INFO = False

Simpleui can be modified through configuration items. The previous rewriting is an idea, including rewriting some template files that django-admin comes with. The same steps are used.

Customized additions in the left sidebar

View official documents

SIMPLEUI_CONFIG = {
    'system_keep': True,
    # 'menu_display': ['Simpleui', '测试', '权限认证', '动态菜单测试'],      # 开启排序和过滤功能, 不填此字段为默认排序和全部显示, 空列表[] 为全部不显示.
    'dynamic': True,  # 设置是否开启动态菜单, 默认为False. 如果开启, 则会在每次用户登陆时动态展示菜单内容
    'menus': [
        {
            'name': '可视化',
            'icon': 'fa-fw fa fa-leaf',
            'models': [{
                'name': '文章报表',
                'icon': 'far fa-surprise',
                'url': 'https://www.baidu.com'
            }]
        },
    ]
}
image-20210517142833166

Other small configurations

# simpleui配置
SIMPLEUI_HOME_INFO = False  # 去掉简介栏
SIMPLEUI_LOGO = 'https://www.liboer.top/static/image/favicon.ico'  # logo

More can go to the official website to view

Import and export plugins

installation

pip3 install django-import-export

use

Go to the app to register

INSTALLED_APPS = [
    'simpleui',
    'import_export',
]

Configure IMPORT_EXPORT_USE_TRANSACTIONS, the default value is False. It determines whether the library will use database transactions in the data import to ensure safety.

# 导入导出
IMPORT_EXPORT_USE_TRANSACTIONS = True

In the admin.py file, you can extend ImportExportModelAdmin to the management class that wants to add the imported and exported tables. This plug-in is actually a rewrite and extension of the django ModelAdmin.

@admin.register(models.Article)
class ArticleAdmin(ImportExportModelAdmin):
    list_display = ['title', category, 'update_time', 'is_display']
    list_editable = ['is_display']
    list_filter = ['title']
    form = ArticleForm
image-20210517144405672

Afterword

My blog is currently running normally. This is the record and summary of my own blog website. If you follow my tutorial, there will generally be no problems, but there will always be bugs. If you encounter problems, welcome to communicate with me.

Finally, if you think this article is useful to you, welcome one-click three-link , as appropriate , thank you!