|
|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
|
|
# 导入Django管理后台相关模块
|
|
|
|
|
from django import forms
|
|
|
|
|
from django.contrib import admin
|
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
@ -5,44 +7,50 @@ from django.urls import reverse
|
|
|
|
|
from django.utils.html import format_html
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
|
|
# Register your models here.
|
|
|
|
|
# 注册模型
|
|
|
|
|
from .models import Article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 文章模型的自定义表单
|
|
|
|
|
class ArticleForm(forms.ModelForm):
|
|
|
|
|
# body = forms.CharField(widget=AdminPagedownWidget())
|
|
|
|
|
# body = forms.CharField(widget=AdminPagedownWidget()) # 可选:使用自定义编辑器
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Article
|
|
|
|
|
fields = '__all__'
|
|
|
|
|
fields = '__all__' # 包含所有字段
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 批量操作:发布文章
|
|
|
|
|
def makr_article_publish(modeladmin, request, queryset):
|
|
|
|
|
queryset.update(status='p')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 批量操作:设为草稿
|
|
|
|
|
def draft_article(modeladmin, request, queryset):
|
|
|
|
|
queryset.update(status='d')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 批量操作:关闭评论
|
|
|
|
|
def close_article_commentstatus(modeladmin, request, queryset):
|
|
|
|
|
queryset.update(comment_status='c')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 批量操作:开启评论
|
|
|
|
|
def open_article_commentstatus(modeladmin, request, queryset):
|
|
|
|
|
queryset.update(comment_status='o')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 设置批量操作的显示名称
|
|
|
|
|
makr_article_publish.short_description = _('Publish selected articles')
|
|
|
|
|
draft_article.short_description = _('Draft selected articles')
|
|
|
|
|
close_article_commentstatus.short_description = _('Close article comments')
|
|
|
|
|
open_article_commentstatus.short_description = _('Open article comments')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 文章模型的管理后台自定义类
|
|
|
|
|
class ArticlelAdmin(admin.ModelAdmin):
|
|
|
|
|
list_per_page = 20
|
|
|
|
|
search_fields = ('body', 'title')
|
|
|
|
|
form = ArticleForm
|
|
|
|
|
list_per_page = 20 # 每页显示数量
|
|
|
|
|
search_fields = ('body', 'title') # 支持搜索的字段
|
|
|
|
|
form = ArticleForm # 使用自定义表单
|
|
|
|
|
list_display = (
|
|
|
|
|
'id',
|
|
|
|
|
'title',
|
|
|
|
|
@ -52,12 +60,12 @@ class ArticlelAdmin(admin.ModelAdmin):
|
|
|
|
|
'views',
|
|
|
|
|
'status',
|
|
|
|
|
'type',
|
|
|
|
|
'article_order')
|
|
|
|
|
list_display_links = ('id', 'title')
|
|
|
|
|
list_filter = ('status', 'type', 'category')
|
|
|
|
|
filter_horizontal = ('tags',)
|
|
|
|
|
exclude = ('creation_time', 'last_modify_time')
|
|
|
|
|
view_on_site = True
|
|
|
|
|
'article_order') # 列表页显示字段
|
|
|
|
|
list_display_links = ('id', 'title') # 可点击进入详情的字段
|
|
|
|
|
list_filter = ('status', 'type', 'category') # 过滤器
|
|
|
|
|
filter_horizontal = ('tags',) # 多对多字段横向筛选
|
|
|
|
|
exclude = ('creation_time', 'last_modify_time') # 编辑页排除字段
|
|
|
|
|
view_on_site = True # 支持“在站点上查看”
|
|
|
|
|
actions = [
|
|
|
|
|
makr_article_publish,
|
|
|
|
|
draft_article,
|
|
|
|
|
|