Django Admin custom style and function

Custom Admin style and function

1 page modify Chinese

1.1 language is set to Chinese

settings.py

LANGUAGE_CODE = 'zh-hans'

Modify the results

1.2 Application Management is set to Chinese

Application /apps.py

from django.apps import AppConfig


class BbssConfig(AppConfig):
    name = 'bbs'
    # 添加下面这句
    verbose_name = 'BBS系统'

Modify the results

1.3 database table is set to Chinese

Application /models.py

class Comment(models.Model):
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    comment_text = models.TextField(max_length=2000)
    author = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
    picture = models.FileField(blank=True, null=True)  # 添加文件类型字段,并默认为空
    pub_date = models.DateTimeField(auto_now_add=True)

    def get_comment_text_md(self):
        """将markdown格式转化为html"""
        return mark_safe(markdown(self.comment_text))

    def __str__(self):
        return self.comment_text

    class Meta:
        verbose_name = '评论'  # 单数时显示内容
        verbose_name_plural = '评论'  # 复数时显示内容

The table below shows the default database are plural form in the background, while the Chinese no plural form, thus both forms are set to the same name

Modify the results

1.4 database table field name changed to Chinese

Application /models.py

class Comment(models.Model):
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE, verbose_name='话题')
    comment_text = models.TextField('评价内容', max_length=2000)
    author = models.ForeignKey(User, default=1, on_delete=models.CASCADE, verbose_name='用户')
    picture = models.FileField('图片', blank=True, null=True)  # 添加文件类型字段,并默认为空
    pub_date = models.DateTimeField('发布时间', auto_now_add=True)

    def get_comment_text_md(self):
        """将markdown格式转化为html"""
        return mark_safe(markdown(self.comment_text))

    def __str__(self):
        return self.comment_text

    class Meta:
        verbose_name = '评论'  # 单数时显示内容
        verbose_name_plural = '评论'  # 复数时显示内容

Just add a general field displays the name of the location parameters can be, and to-many relationship to keyword parameters verbose_nameand key parameters to be placed behind the position parameter

Modify the results

2 Modify the background style

Django-grappelli use third-party applications to modify admin style
GitHub: https://github.com/sehmaschine/django-grappelli

Documentation: https://django-grappelli.readthedocs.io/en/latest/quickstart.html

Other tools: https://djangopackages.org/grids/g/admin-interface/

2.1 Installation

pip install django-grappelli

2.2 Importing project

settings.py

INSTALLED_APPS = [
    'accounts.apps.AccountsConfig',
    'polls.apps.PollsConfig',
    'bbs.apps.BbssConfig',
    'grappelli',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

2.3 Import URL

Project / urls.py

urlpatterns = [
    path('grappelli', include('grappelli.urls')),
    path('admin/', admin.site.urls),
    path('', include('bbs.urls')),
    path('accounts/', include('accounts.urls')),
]

2.4 unified collection of static files in one place

settings.py add

# 收集静态文件统一存放的根路径
STATIC_ROOT = os.path.join(BASE_DIR, 'static-files')

Excuting an order

 python manage.py collectstatic

Automatic generated

Services will start again found management page has been modified

2.5 Custom Title

settings.py add

# 后台自定义标题
GRAPPELLI_ADMIN_TITLE = 'Z-BBS ADMIN'

refresh page

2.6 admin open tabs function

Application / admin.py

from django.contrib import admin

# Register your models here.
from .models import Topic, Comment


class TopicAdmin(admin.ModelAdmin):
    list_display = ('topic_text', 'author', 'pub_date')
    search_fields = ('topic_text', 'author')
    list_editable = ('author',)
    list_per_page = 10


class CommentAdmin(admin.ModelAdmin):
    list_display = ( 'comment_text', 'author', 'pub_date', 'topic')
    search_fields = ('comment_text', 'author')
    list_editable = ('author',)
    list_per_page = 10

2.7 admin open the filter function

Application / admin.py

class TopicAdmin(admin.ModelAdmin):
    list_display = ('topic_text', 'author', 'pub_date')
    list_filter = ('topic_text', 'author', 'pub_date')
    search_fields = ('topic_text',)
    list_editable = ('author',)
    list_per_page = 10


class CommentAdmin(admin.ModelAdmin):
    list_display = ( 'comment_text', 'author', 'pub_date', 'topic')
    list_filter = ('comment_text', 'author', 'pub_date', 'topic')
    search_fields = ('comment_text',)
    list_editable = ('author',)
    list_per_page = 10

I remember after turning force a refresh the page (ctrl + shift + r), reload js and css code

Guess you like

Origin www.cnblogs.com/izbw/p/11129895.html