feat: logging替换print/N+1优化/站内搜索/主题过渡一致/404-500页/评分UI/空状态/评论回显/PostListView排序 - #45
Open
xiokuai wants to merge 3 commits into
Open
feat: logging替换print/N+1优化/站内搜索/主题过渡一致/404-500页/评分UI/空状态/评论回显/PostListView排序#45xiokuai wants to merge 3 commits into
xiokuai wants to merge 3 commits into
Conversation
P1 将 getTheme 移入 head 的 IIFE 内, 导致 body 末尾脚本 调用 getTheme() 时抛出 ReferenceError, 整个脚本块崩溃, 主题切换按钮/图标更新/返回顶部按钮全部失效。 修复: - head 中 getTheme 提升为全局函数, body 末尾可正常访问 - markdown/hljs 的 disabled 切换从 DOMContentLoaded 改为 在 link 元素之后立即执行, 进一步消除 FOUC 窗口 基于 fix/p1-bugs 分支, 依赖 PR aba2222#43。
- Resolve base.html conflict with origin/main: keep minimal FOUC init pattern - Promote getTheme to window.getTheme inside head IIFE, so the markdown/hljs stylesheet disabled switcher (also in head) can access it without ReferenceError - Remove duplicate getTheme declaration at end of body (uses window global now) - settings.py: SECRET_KEY one-liner, CORS_ALLOWED_ORIGINS list comprehension (per aba2222#43 review) - md_editor.html: remove manual theme sync script (md_editor follows data-bs-theme natively, per aba2222#43 review) - Test suite: 12/12 passing
1. print(forms.errors) → Django logging: 新增LOGGING配置,统一使用logger.warning/exception
2. N+1查询优化:index/search/collection_list使用annotate+select_related,避免循环内查询
3. 空状态体验:所有列表页(index/post_list/collection_list/search)统一Bootstrap alert空状态UI
4. 评论表单未保存内容丢失:校验失败时返回原form对象回显用户输入,同时messages提示保留
5. PostListView排序:默认按created_at倒序,支持q搜索参数
6. Item评分页&首页区块UI:rate_item.html星级展示+平均/用户评分,index.html&search.html Item卡片带星星
7. messages覆盖{info,success,warning,error}:登录/注册/发帖/删帖/评分/评论等全流程补齐
8. 站内搜索:search_view支持Post/Collection/Item多模型Q查询,search.html结果页,导航栏搜索入口
9. 主题圆圈扩散+body背景色过渡一致:theme-reveal动画520ms,55%处(286ms)切换主题配合body 280ms transition
10. 404/500自定义错误页:友好提示+快捷导航按钮+handler404/500配置
Owner
|
感谢你的贡献!为方便代码审查和维护,请将这些改动按照主要内容拆分成多个独立的 PR。每个 PR 应尽量只关注一个变更。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
本次 PR 包含以下修复和优化(共 9 项用户需求 + 附带修复)
1. print(forms.errors) → Django logging 替换
lean_forum/settings.py新增 LOGGING 配置:root WARNING 级 +forumlogger DEBUG/INFO 级 +djangologger INFO 级,统一{asctime} {levelname} {name} {message}格式forum/views.py全部原先的print(forms.errors)统一改为:logger.warning("xxx 表单校验失败: user=%s errors=%s", ...)用于表单类失败logger.exception("xxx 失败")用于 webpush / 注册 等异常捕获块2. N+1 查询优化(高命中页面)
Item.objects.annotate(avg_score=Avg('rating__score'), rating_count=Count('rating')),模板直接用 item.avg_score / item.rating_count,不再调用方法触发额外查询Post.objects.select_related('author').order_by('-created_at')[:6],避免循环里 post.author 再查{{ collection.collection_posts.count }}→{{ collection.post_count }},消灭列表 N+1collection.collection_posts.select_related('post', 'post__author')合辑内帖子 + 作者一次取出3. 页面 404 / 空状态体验
templates/404.html:templates/500.html:lean_forum/urls.py配置handler404 = 'forum.views.custom_404_view'/handler500,views.py 新增两个对应 view4. 评论表单显示 / 未保存内容丢失
PostDetailView.post()原先校验失败用 redirect → 丢失用户输入:改为把校验失败的forms对象传入_post_detail_context(comment_form=forms),直接render(request, 'forum/post_detail.html', ctx),模板{{ forms.as_p }}自然回显collection_post_detail()POST 评论分支同样改为 render 回显post_create/post_edit/comment_edit/collection_create/collection_edit原本就保留 form 实例,本次再确认 + messages 补齐「已保留您当前的输入」for field, errs in forms.errors.items(): messages.warning(...)列出字段级错误5. PostListView 无排序
get_queryset()显式.order_by('-created_at'),不再依赖表的默认顺序?q=搜索过滤后仍保持倒序get_context_data注入q与post_count(= 当前过滤条件下 count),模板头部正确显示搜索词与数量 / 清除按钮6. Item 相关:评分页 & 首页区块没对应 UI
平均分 X.X (X 人评).star { color:#d4a017 }/.star.is-muted { color: var(--bs-border-color) }统一样式7. messages 未全部覆盖 {info, success, warning, error}
完整覆盖四类:
8. 搜索(站内检索)
Q(...) | Q(...)icontains 过滤path('search/', views.search_view, name='search')9. 主题圆圈扩散 + body 背景色过渡不一致
原先切换主题时,圆圈扩散和 body 背景色不同步,要么先切要么后切,有明显视觉割裂。
base CSS 统一:
body { transition: background-color 280ms cubic-bezier(0.4, 0, 0.2, 1), color 280ms ... }过渡与缓动固定.theme-reveal固定层:position:fixed; inset:0; z-index:9999; pointer-events:none,clip-path: circle(0 at var(--reveal-x/y)),背景色--theme-reveal-color.theme-reveal.is-playing触发动画:520ms cubic-bezier(0.4,0,0.2,1) forwardstheme-reveal-expand:0% circle(0) opacity 1 → 55% 仍 opacity 1 → 100% circle(150vmax) opacity 0。刻意让 55% 时刻圆圈已盖过主体但还未淡出JS playThemeReveal(next, originEl):
_revealLock防抖,防止连点prefers-reduced-motion: reduce/ 无 originEl → 直接 syncTheme,不播动画setProperty('--reveal-x', cx+'px')/--reveal-y/--theme-reveal-color(= getThemeBg(next) 读 CSS 变量)is-playing启动动画同步辅助函数 syncTheme:统一切换 data-bs-theme + 更新太阳/月亮图标 + markdown CSS disabled + highlight.js CSS disabled(head 内 IIFE 已先处理避免 FOUC,body 末尾初始调用一次再建立事件)
主题按钮点击 → playThemeReveal 传入 themeToggle 元素 → 圆圈从按钮中心扩散。
额外附带修复
之前
created = Rating.objects.update_or_create(...)[1] is False把 update_or_create 返回的 created 反过来用了,导致每次提示语都是「已更新 / 已创建」错位。改为:_obj, created = Rating.objects.update_or_create(...)直接取原生 created 语义 → if created 提示「已为 XX 打 X 星」else 提示「已更新评分为 X 星」。