From 1fec2a2aa7ca6416f0c4b22a6ecf8e05bc787691 Mon Sep 17 00:00:00 2001 From: aba2222 Date: Wed, 25 Feb 2026 16:44:03 +0800 Subject: [PATCH 1/6] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E5=9B=BE=E6=A0=87=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- forum/templates/base.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forum/templates/base.html b/forum/templates/base.html index cec2719..76a6596 100644 --- a/forum/templates/base.html +++ b/forum/templates/base.html @@ -122,7 +122,7 @@ function updateThemeIcon() { const icon = document.getElementById('theme-icon'); const isDark = document.documentElement.getAttribute('data-bs-theme') === 'dark'; - icon.className = 'bi ' + (isDark ? 'bi-sun' : 'bi-moon-stars'); + icon.className = 'bi ' + (isDark ? 'bi-moon' : 'bi-sun'); } updateThemeIcon(); From e9c5dae2a05fbad4eece78ce3a825d696ec0badc Mon Sep 17 00:00:00 2001 From: aba2222 Date: Wed, 25 Feb 2026 16:52:24 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fix:=20=E5=B0=86title=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E6=96=87=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- forum/templates/forum/collection_form.html | 4 ++-- forum/views.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/forum/templates/forum/collection_form.html b/forum/templates/forum/collection_form.html index 7a483bb..86118f6 100644 --- a/forum/templates/forum/collection_form.html +++ b/forum/templates/forum/collection_form.html @@ -1,9 +1,9 @@ {% extends 'base.html' %} -{% block title %}{{ title }}{% endblock %} +{% block title %}创建合集{% endblock %} {% block content %} -

{{ title }}

+

创建合集

{% csrf_token %} diff --git a/forum/views.py b/forum/views.py index 1c08d7b..d7822ea 100644 --- a/forum/views.py +++ b/forum/views.py @@ -254,7 +254,7 @@ def collection_create(request): if form.is_valid(): form.save() return redirect('collection_list') - return render(request, 'forum/collection_form.html', {'form': form, 'title': '创建合集'}) + return render(request, 'forum/collection_form.html', {'form': form}) def collection_detail(request, collection_id): From a1c25a366370c13403326166f2650d8467ac7f6c Mon Sep 17 00:00:00 2001 From: aba2222 Date: Thu, 26 Feb 2026 11:51:47 +0800 Subject: [PATCH 3/6] =?UTF-8?q?collection=E7=BB=A7=E6=89=BFMarkdownModel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- forum/form.py | 6 +++--- forum/models.py | 20 +------------------- forum/templates/forum/collection_detail.html | 4 ++-- forum/templates/forum/collection_list.html | 4 ++-- 4 files changed, 8 insertions(+), 26 deletions(-) diff --git a/forum/form.py b/forum/form.py index 05ff949..001e88d 100644 --- a/forum/form.py +++ b/forum/form.py @@ -37,14 +37,14 @@ def __init__(self, *args, user=None, **kwargs): class Meta: model = Collection - fields = ['name', 'description'] + fields = ['name', 'content'] labels = { 'name': '合集名称', - 'description': '描述', + 'content': '描述', } widgets = { 'name': forms.TextInput(attrs={'class': 'form-control'}), - 'description': forms.Textarea(attrs={'rows': 3, 'class': 'form-control'}), + 'content': forms.Textarea(attrs={'rows': 3, 'class': 'form-control'}), } def save(self, commit=True): diff --git a/forum/models.py b/forum/models.py index 0880d48..d9e5afd 100644 --- a/forum/models.py +++ b/forum/models.py @@ -3,9 +3,6 @@ from .markdown import MarkdownModel -import markdown -import bleach - # Create your models here. class Item(MarkdownModel): @@ -47,11 +44,9 @@ def __str__(self): return f"{self.author} comment {self.post}" -class Collection(models.Model): +class Collection(MarkdownModel): owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='collections') name = models.CharField(max_length=100) - description = models.TextField(blank=True, default='') - description_html = models.TextField(editable=False, blank=True) created_at = models.DateTimeField(auto_now_add=True) class Meta: @@ -60,19 +55,6 @@ class Meta: def __str__(self): return self.name - def save(self, *args, **kwargs): - if self.description: - html = markdown.markdown(self.description, extensions=MarkdownModel.MARKDOWN_EXTENSIONS) - self.description_html = bleach.clean( - html, - tags=MarkdownModel.allowed_tags, - attributes=MarkdownModel.allowed_attrs, - protocols=MarkdownModel.ALLOWED_PROTOCOLS, - ) - else: - self.description_html = '' - super().save(*args, **kwargs) - class CollectionPost(models.Model): collection = models.ForeignKey(Collection, on_delete=models.CASCADE, related_name='collection_posts') diff --git a/forum/templates/forum/collection_detail.html b/forum/templates/forum/collection_detail.html index 465b31f..e460b55 100644 --- a/forum/templates/forum/collection_detail.html +++ b/forum/templates/forum/collection_detail.html @@ -4,8 +4,8 @@ {% block content %}

{{ collection.name }}

-{% if collection.description_html %} -
{{ collection.description_html|safe }}
+{% if collection.content_html %} +
{{ collection.content_html|safe }}
{% endif %}

diff --git a/forum/templates/forum/collection_list.html b/forum/templates/forum/collection_list.html index 9e9c854..ebc3902 100644 --- a/forum/templates/forum/collection_list.html +++ b/forum/templates/forum/collection_list.html @@ -14,8 +14,8 @@

合集

{{ collection.name }} by {{ collection.owner.username }} - {% if collection.description %} -
{{ collection.description }} + {% if collection.content %} +
{{ collection.content }} {% endif %}
{{ collection.collection_posts.count }} 篇 From 1274bb162162f2ea8210242bf2eccbe41b079338 Mon Sep 17 00:00:00 2001 From: aba2222 Date: Thu, 26 Feb 2026 11:55:03 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=88=E9=9B=86?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E8=87=AA=E5=8A=A8=E6=88=AA=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- forum/templates/forum/collection_list.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forum/templates/forum/collection_list.html b/forum/templates/forum/collection_list.html index ebc3902..d7d8769 100644 --- a/forum/templates/forum/collection_list.html +++ b/forum/templates/forum/collection_list.html @@ -15,7 +15,7 @@

合集

{{ collection.name }} by {{ collection.owner.username }} {% if collection.content %} -
{{ collection.content }} +
{{ collection.content | truncatechars:100}} {% endif %} {{ collection.collection_posts.count }} 篇 From b68b986f9a2b125a515220b9f17ba573d26a082c Mon Sep 17 00:00:00 2001 From: aba2222 Date: Thu, 26 Feb 2026 14:37:53 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BC=98=E5=8C=96post=5Flist=E6=80=A7?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- forum/templates/forum/post_list.html | 10 +++++----- forum/views.py | 29 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/forum/templates/forum/post_list.html b/forum/templates/forum/post_list.html index 8b0d24b..a5bc6a9 100644 --- a/forum/templates/forum/post_list.html +++ b/forum/templates/forum/post_list.html @@ -10,14 +10,14 @@

所有文章

{% if item.type == 'collection' %}
  • - {{ item.obj.name }} - — 合集 · {{ item.obj.owner.username }} - · {{ item.obj.collection_posts.count }} 篇文章 + {{ item.name }} + — 合集 · {{ item.owner.username }} + · {{ item.collection_posts.count }} 篇文章
  • {% else %}
  • - {{ item.obj.title }} - 作者:{{ item.obj.author }} {{ item.obj.views }} - 去查看 + {{ item.title }} - 作者:{{ item.author.username }} {{ item.views }} + 去查看
  • {% endif %} {% empty %} diff --git a/forum/views.py b/forum/views.py index d7822ea..eb53f61 100644 --- a/forum/views.py +++ b/forum/views.py @@ -1,5 +1,5 @@ import json, re, random -from django.db.models import F +from django.db.models import F, CharField, Subquery, Value from django.db import models as db_models from django.http import JsonResponse from django.urls import reverse_lazy @@ -22,29 +22,30 @@ def index(request): items = Item.objects.all() - posts = Post.objects.all().order_by('-created_at')[:5] + posts = Post.objects.all()[:5] return render(request, 'forum/index.html', {'items': items, 'posts' : posts}) def post_list(request): # IDs of posts that belong to any collection - collected_post_ids = CollectionPost.objects.values_list('post_id', flat=True) + collected_post_ids = CollectionPost.objects.values('post_id') # Standalone posts (not in any collection) - standalone_posts = [ - {'type': 'post', 'obj': p, 'date': p.created_at} - for p in Post.objects.exclude(id__in=collected_post_ids) - ] + standalone_posts = Post.objects.exclude( + id__in=Subquery(collected_post_ids) + ).annotate( + type=Value('post', output_field=CharField()) + ) # Collections as items - collection_items = [ - {'type': 'collection', 'obj': c, 'date': c.created_at} - for c in Collection.objects.all() - ] + collection_items = Collection.objects.annotate( + type=Value('collection', output_field=CharField()) + ) - # Merge and sort by date descending - items = sorted(standalone_posts + collection_items, key=lambda x: x['date'], reverse=True) + combined = list(standalone_posts) + list(collection_items) - paginator = Paginator(items, 20) + combined.sort(key=lambda x: x.created_at, reverse=True) + + paginator = Paginator(combined, 20) page_obj = paginator.get_page(request.GET.get('page', 1)) return render(request, 'forum/post_list.html', { From af5f4a72b73af28e2c86582af333b190a61e3cda Mon Sep 17 00:00:00 2001 From: aba2222 Date: Thu, 26 Feb 2026 14:59:45 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BD=BF=E7=94=A8SQL=20UNION=E8=80=8C?= =?UTF-8?q?=E4=B8=8D=E6=98=AFPython=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- forum/views.py | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/forum/views.py b/forum/views.py index eb53f61..c67d138 100644 --- a/forum/views.py +++ b/forum/views.py @@ -26,27 +26,47 @@ def index(request): return render(request, 'forum/index.html', {'items': items, 'posts' : posts}) def post_list(request): - # IDs of posts that belong to any collection - collected_post_ids = CollectionPost.objects.values('post_id') - # Standalone posts (not in any collection) standalone_posts = Post.objects.exclude( - id__in=Subquery(collected_post_ids) + id__in = CollectionPost.objects.values_list('post_id', flat=True) # IDs of posts that belong to any collection ).annotate( type=Value('post', output_field=CharField()) - ) + ).values("type", "id", "created_at").order_by() # Collections as items collection_items = Collection.objects.annotate( type=Value('collection', output_field=CharField()) - ) - - combined = list(standalone_posts) + list(collection_items) + ).values("type", "id", "created_at").order_by() - combined.sort(key=lambda x: x.created_at, reverse=True) + combined = standalone_posts.union( + collection_items, + all=True + ).order_by('-created_at') paginator = Paginator(combined, 20) - page_obj = paginator.get_page(request.GET.get('page', 1)) + page_items = paginator.get_page(request.GET.get('page', 1)) + + page_post_ids = [] + page_collection_ids = [] + for row in page_items: + if row["type"] == "post": + page_post_ids.append(row["id"]) + else: + page_collection_ids.append(row["id"]) + + posts = Post.objects.in_bulk(page_post_ids) + collections = Collection.objects.in_bulk(page_collection_ids) + + + page_obj = [] + for row in page_items: + if row["type"] == "post": + obj = posts.get(row["id"]) + else: + obj = collections.get(row["id"]) + + obj.type = row["type"] + page_obj.append(obj) return render(request, 'forum/post_list.html', { 'page_obj': page_obj,