Add change detail_view long in admin
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from .models import Post, Category
|
from .models import Post, Category, SiteSettings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from mdeditor.widgets import MDEditorWidget
|
from mdeditor.widgets import MDEditorWidget
|
||||||
|
|
||||||
@@ -16,6 +16,15 @@ class PostAdmin(admin.ModelAdmin):
|
|||||||
search_fields = ('title', 'content')
|
search_fields = ('title', 'content')
|
||||||
|
|
||||||
|
|
||||||
|
class SiteSettingsAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('summary_length',)
|
||||||
|
|
||||||
|
def has_add_permission(self, request):
|
||||||
|
# 限制只能有一个站点设置实例
|
||||||
|
return not SiteSettings.objects.exists()
|
||||||
|
|
||||||
|
|
||||||
# 注册自定义的PostAdmin
|
# 注册自定义的PostAdmin
|
||||||
admin.site.register(Post, PostAdmin)
|
admin.site.register(Post, PostAdmin)
|
||||||
admin.site.register(Category)
|
admin.site.register(Category)
|
||||||
|
admin.site.register(SiteSettings, SiteSettingsAdmin)
|
||||||
24
myblog/blog/migrations/0008_sitesettings.py
Normal file
24
myblog/blog/migrations/0008_sitesettings.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Generated by Django 5.1 on 2025-07-27 15:27
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('blog', '0007_post_category'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='SiteSettings',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('summary_length', models.IntegerField(default=50, help_text='文章摘要字符长度')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'verbose_name': '站点设置',
|
||||||
|
'verbose_name_plural': '站点设置',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -19,6 +19,18 @@ class Category(models.Model):
|
|||||||
verbose_name_plural = "Categories"
|
verbose_name_plural = "Categories"
|
||||||
|
|
||||||
|
|
||||||
|
# 添加站点设置模型
|
||||||
|
class SiteSettings(models.Model):
|
||||||
|
summary_length = models.IntegerField(default=50, help_text="文章摘要字符长度")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = "站点设置"
|
||||||
|
verbose_name_plural = "站点设置"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "站点设置"
|
||||||
|
|
||||||
|
|
||||||
class Post(models.Model):
|
class Post(models.Model):
|
||||||
title = models.CharField(max_length=100)
|
title = models.CharField(max_length=100)
|
||||||
content = MDTextField() # ✅ 改成这里
|
content = MDTextField() # ✅ 改成这里
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from django.shortcuts import render, get_object_or_404
|
from django.shortcuts import render, get_object_or_404
|
||||||
from .models import Post, Category
|
from .models import Post, Category, SiteSettings
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@@ -17,6 +17,13 @@ def index(request):
|
|||||||
# 获取搜索类型参数
|
# 获取搜索类型参数
|
||||||
search_type = request.GET.get('search_type', 'all')
|
search_type = request.GET.get('search_type', 'all')
|
||||||
|
|
||||||
|
# 获取站点设置,如果不存在则使用默认值
|
||||||
|
try:
|
||||||
|
site_settings = SiteSettings.objects.first()
|
||||||
|
summary_length = site_settings.summary_length if site_settings else 50
|
||||||
|
except SiteSettings.DoesNotExist:
|
||||||
|
summary_length = 50
|
||||||
|
|
||||||
# 根据分类和搜索关键词筛选文章
|
# 根据分类和搜索关键词筛选文章
|
||||||
if query:
|
if query:
|
||||||
# 根据搜索类型执行不同的搜索
|
# 根据搜索类型执行不同的搜索
|
||||||
@@ -34,12 +41,12 @@ def index(request):
|
|||||||
|
|
||||||
posts = posts.order_by('-publish_date').distinct()
|
posts = posts.order_by('-publish_date').distinct()
|
||||||
|
|
||||||
# 为每篇文章添加摘要(前50个字符)
|
# 为每篇文章添加摘要(根据设置的字符长度)
|
||||||
for post in posts:
|
for post in posts:
|
||||||
# 移除HTML标签并截取前50个字符作为摘要
|
# 移除HTML标签并截取前N个字符作为摘要
|
||||||
import re
|
import re
|
||||||
clean_content = re.sub(r'<[^>]+>', '', post.get_markdown_content())
|
clean_content = re.sub(r'<[^>]+>', '', post.get_markdown_content())
|
||||||
post.summary = clean_content[:50] + '...' if len(clean_content) > 50 else clean_content
|
post.summary = clean_content[:summary_length] + '...' if len(clean_content) > summary_length else clean_content
|
||||||
|
|
||||||
return render(request, 'blog/index.html', {
|
return render(request, 'blog/index.html', {
|
||||||
'posts': posts,
|
'posts': posts,
|
||||||
|
|||||||
Reference in New Issue
Block a user