完善了首页和关于前端页面,以及解决了图片存储方案问题

This commit is contained in:
2026-10-20 04:53:06 +08:00
parent 16a75807ee
commit 7e45fa620e
20 changed files with 625 additions and 96 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+9 -1
View File
@@ -3,7 +3,7 @@ Django 自带的管理后台配置
在这里注册模型后,就能在 /admin/ 里管理数据
"""
from django.contrib import admin
from .models import Post
from .models import Post,SiteInfo
@admin.register(Post)
@@ -21,3 +21,11 @@ class PostAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('title',)}
# 列表页每页显示数量
list_per_page = 20
@admin.register(SiteInfo)
class SiteInfoAdmin(admin.ModelAdmin):
list_display = ['about_title', 'updated_at']
def has_add_permission(self, request):
# 只允许创建一条记录
return not SiteInfo.objects.exists()
+15
View File
@@ -0,0 +1,15 @@
import urllib.parse
class UnicodeSlugConverter:
"""
支持中文 slug 的路径转换器
匹配除 / 之外的任意字符
"""
regex = '[^/]+'
def to_python(self, value):
return urllib.parse.unquote(value)
def to_url(self, value):
return urllib.parse.quote(value)
+75
View File
@@ -0,0 +1,75 @@
# Generated by Django 4.2 on 2026-10-19 20:44
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("blog", "0002_tag_category_post_categories_post_tags"),
]
operations = [
migrations.CreateModel(
name="SiteInfo",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"about_title",
models.CharField(
default="关于我", max_length=200, verbose_name="关于页标题"
),
),
(
"about_content",
models.TextField(
help_text="支持 HTML,可以写自我介绍、社交链接等",
verbose_name="关于我内容",
),
),
(
"avatar",
models.ImageField(
blank=True, null=True, upload_to="site/", verbose_name="头像"
),
),
(
"github_url",
models.URLField(blank=True, default="", verbose_name="GitHub 链接"),
),
(
"email",
models.EmailField(
blank=True, default="", max_length=254, verbose_name="邮箱"
),
),
(
"wechat",
models.CharField(
blank=True, default="", max_length=100, verbose_name="微信号"
),
),
(
"site_description",
models.TextField(blank=True, default="", verbose_name="站点简介"),
),
(
"updated_at",
models.DateTimeField(auto_now=True, verbose_name="更新时间"),
),
],
options={
"verbose_name": "站点信息",
"verbose_name_plural": "站点信息",
"db_table": "blog_site_info",
},
),
]
+51
View File
@@ -162,3 +162,54 @@ class Post(models.Model):
def get_absolute_url(self):
return reverse('blog:post_detail', kwargs={'slug': self.slug})
class SiteInfo(models.Model):
"""
站点信息(关于页、站点配置)
只创建一条记录,通过 admin 编辑
"""
about_title = models.CharField(
max_length=200,
default='关于我',
verbose_name='关于页标题'
)
about_content = models.TextField(
verbose_name='关于我内容',
help_text='支持 HTML,可以写自我介绍、社交链接等'
)
avatar = models.ImageField(
upload_to='site/',
blank=True,
null=True,
verbose_name='头像'
)
# 可选:社交链接
github_url = models.URLField(blank=True, default='', verbose_name='GitHub 链接')
email = models.EmailField(blank=True, default='', verbose_name='邮箱')
wechat = models.CharField(max_length=100, blank=True, default='', verbose_name='微信号')
site_description = models.TextField(
blank=True,
default='',
verbose_name='站点简介'
)
updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
class Meta:
verbose_name = '站点信息'
verbose_name_plural = '站点信息'
db_table = 'blog_site_info'
def __str__(self):
return self.about_title
@classmethod
def get_site_info(cls):
"""获取唯一的站点信息,没有就创建默认"""
obj, created = cls.objects.get_or_create(pk=1)
return obj
+55
View File
@@ -0,0 +1,55 @@
{% extends "base.html" %}
{% block title %}关于 - 人,是一根会思考的芦苇{% endblock %}
{% block content %}
<div style="margin-bottom: 2rem;">
<!-- 头像 + 标题 -->
{% if site_info.avatar %}
<div style="text-align: center; margin-bottom: 2rem;">
<img src="{{ site_info.avatar.url }}" alt="头像"
style="width: 120px; height: 120px; border-radius: 50%; object-fit: cover; border: 2px solid var(--border);">
</div>
{% endif %}
<h2 style="
font-size: 1.3rem;
font-weight: 600;
color: var(--text);
margin-bottom: 1.5rem;
text-align: center;
">{{ site_info.about_title }}</h2>
<!-- 关于内容(支持 HTML) -->
<div class="post-body" style="margin-bottom: 2rem;">
{{ site_info.about_content|safe }}
</div>
<!-- 社交链接 -->
{% if site_info.github_url or site_info.email or site_info.wechat %}
<div style="
margin-top: 2rem;
padding-top: 1.5rem;
border-top: 1px solid var(--border);
font-size: 0.9rem;
color: var(--text-light);
font-family: -apple-system, 'Noto Sans SC', sans-serif;
">
<p style="margin-bottom: 0.5rem; font-weight: 600; color: var(--text);">联系方式</p>
{% if site_info.email %}
<p style="margin-bottom: 0.3rem;">📧 <a href="mailto:{{ site_info.email }}" style="color: var(--text-light); text-decoration: none;">{{ site_info.email }}</a></p>
{% endif %}
{% if site_info.github_url %}
<p style="margin-bottom: 0.3rem;">🐙 <a href="{{ site_info.github_url }}" target="_blank" rel="noopener" style="color: var(--text-light); text-decoration: none;">{{ site_info.github_url }}</a></p>
{% endif %}
{% if site_info.wechat %}
<p style="margin-bottom: 0.3rem;">💬 微信:{{ site_info.wechat }}</p>
{% endif %}
</div>
{% endif %}
<div style="margin-top: 2rem; padding-top: 1.5rem; border-top: 1px solid var(--border); font-size: 0.85rem; color: var(--text-muted); font-family: -apple-system, 'Noto Sans SC', sans-serif;">
<a href="/" style="color: var(--text-light); text-decoration: none;">← 返回首页</a>
</div>
</div>
{% endblock %}
+28 -6
View File
@@ -3,14 +3,18 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}郭安的小站{% endblock %}</title>
<title>{% block title %}人,是一根会思考的芦苇{% endblock %}</title>
<!-- highlight.js 样式(护眼低对比主题) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/github.min.css">
<style>
/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
/* ===== 基础变量 ===== */
/* ===== 基础变量(✅ 背景改为米黄色) ===== */
:root {
--bg: #fefefe;
--bg: #fdfaf3; /* ✅ 米黄色背景 */
--text: #1a1a1a;
--text-light: #6b6b6b;
--text-muted: #9b9b9b;
@@ -183,8 +187,9 @@
margin-bottom: 0.4em;
}
/* ===== 代码块(✅ 优化 + highlight.js 兼容) ===== */
.post-body pre {
background: var(--code-bg);
background: #f4f4f4;
padding: 1.2em;
border-radius: var(--radius);
overflow-x: auto;
@@ -198,12 +203,19 @@
font-size: 0.9em;
}
.post-body p code, .post-body li code {
.post-body p code,
.post-body li code {
background: var(--code-bg);
padding: 0.15em 0.4em;
border-radius: 3px;
}
/* highlight.js 代码块微调 */
.post-body pre code.hljs {
background: transparent;
padding: 0;
}
/* ===== 分类/标签 ===== */
.tags, .categories {
margin-top: 3rem;
@@ -248,7 +260,7 @@
<body>
<div class="container">
<header>
<h1><a href="/">郭安的小站</a></h1>
<h1><a href="/">人,是一根会思考的芦苇</a></h1>
<nav>
<a href="/">首页</a>
<a href="/archive/">归档</a>
@@ -264,5 +276,15 @@
<p>© 2024 郭安的小站 · 用 Django 构建</p>
</footer>
</div>
<!-- highlight.js 核心脚本 -->
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/highlight.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('pre code').forEach(function (block) {
hljs.highlightElement(block);
});
});
</script>
</body>
</html>
-23
View File
@@ -1,23 +0,0 @@
{% extends "base.html" %}
{% load static %}
{% block title %}郭安的小站{% endblock %}
{% block content %}
{% for post in posts %}
<div class="post-item">
<h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<div class="post-date">{{ post.published_at|date:"Y年m月d日" }}</div>
{% if post.excerpt %}
<div class="post-summary">{{ post.excerpt }}</div>
{% endif %}
</div>
{% empty %}
<div class="post-item" style="text-align: center; padding: 4rem 0; color: var(--text-muted);">
<p style="margin-bottom: 0.5rem;">还没有文章</p>
<p style="font-size: 0.9rem;">
<a href="/admin/" style="color: var(--text-light); text-decoration: underline; text-decoration-color: var(--border); text-underline-offset: 3px;">去管理后台写一篇</a>
</p>
</div>
{% endfor %}
{% endblock %}
+87
View File
@@ -0,0 +1,87 @@
{% extends "base.html" %}
{% block title %}{{ category.name }} - 郭安的小站{% endblock %}
{% block content %}
<div style="margin-bottom: 2rem;">
<h2 style="
font-size: 1.1rem;
font-weight: 600;
color: var(--text);
margin-bottom: 0.3rem;
">{{ category.name }}</h2>
{% if category.description %}
<p style="font-size: 0.9rem; color: var(--text-muted); margin-bottom: 1.5rem;">{{ category.description }}</p>
{% endif %}
{% for post in posts %}
<div class="post-item">
<h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<div class="post-date">{{ post.published_at|date:"Y年m月d日" }}</div>
{% if post.excerpt %}
<div class="post-summary">{{ post.excerpt }}</div>
{% endif %}
</div>
{% empty %}
<div class="post-item" style="text-align: center; padding: 4rem 0; color: var(--text-muted);">
<p>这个分类下还没有文章</p>
</div>
{% endfor %}
<!-- 分页 -->
{% if posts.paginator.num_pages > 1 %}
<nav style="
margin-top: 3rem;
text-align: center;
font-family: -apple-system, 'Noto Sans SC', sans-serif;
font-size: 0.9rem;
">
{% if posts.has_previous %}
<a href="?page=1" style="margin-right: 0.6rem; color: var(--text-light); text-decoration: none;">« 首页</a>
<a href="?page={{ posts.previous_page_number }}" style="margin-right: 1rem; color: var(--text-light); text-decoration: none;">‹ 上一页</a>
{% else %}
<span style="margin-right: 0.6rem; color: var(--text-muted);">« 首页</span>
<span style="margin-right: 1rem; color: var(--text-muted);">‹ 上一页</span>
{% endif %}
{% for num in posts.paginator.page_range %}
{% if posts.paginator.num_pages <= 9 %}
{% if num == posts.number %}
<span style="margin: 0 0.3rem; color: var(--bg); background: var(--text); padding: 0.2rem 0.6rem; border-radius: var(--radius); font-weight: 600;">{{ num }}</span>
{% else %}
<a href="?page={{ num }}" style="margin: 0 0.3rem; color: var(--text-light); text-decoration: none;">{{ num }}</a>
{% endif %}
{% else %}
{% if num == 1 or num == posts.paginator.num_pages %}
{% if num == posts.number %}
<span style="margin: 0 0.3rem; color: var(--bg); background: var(--text); padding: 0.2rem 0.6rem; border-radius: var(--radius); font-weight: 600;">{{ num }}</span>
{% else %}
<a href="?page={{ num }}" style="margin: 0 0.3rem; color: var(--text-light); text-decoration: none;">{{ num }}</a>
{% endif %}
{% elif num >= posts.number|add:'-2' and num <= posts.number|add:'2' %}
{% if num == posts.number %}
<span style="margin: 0 0.3rem; color: var(--bg); background: var(--text); padding: 0.2rem 0.6rem; border-radius: var(--radius); font-weight: 600;">{{ num }}</span>
{% else %}
<a href="?page={{ num }}" style="margin: 0 0.3rem; color: var(--text-light); text-decoration: none;">{{ num }}</a>
{% endif %}
{% elif num == posts.number|add:'-3' or num == posts.number|add:'3' %}
<span style="margin: 0 0.2rem; color: var(--text-muted);">…</span>
{% endif %}
{% endif %}
{% endfor %}
{% if posts.has_next %}
<a href="?page={{ posts.next_page_number }}" style="margin-left: 1rem; color: var(--text-light); text-decoration: none;">下一页 ›</a>
<a href="?page={{ posts.paginator.num_pages }}" style="margin-left: 0.6rem; color: var(--text-light); text-decoration: none;">末页 »</a>
{% else %}
<span style="margin-left: 1rem; color: var(--text-muted);">下一页 ›</span>
<span style="margin-left: 0.6rem; color: var(--text-muted);">末页 »</span>
{% endif %}
</nav>
{% endif %}
<div style="margin-top: 2rem; padding-top: 1.5rem; border-top: 1px solid var(--border); font-size: 0.85rem; color: var(--text-muted); font-family: -apple-system, 'Noto Sans SC', sans-serif;">
<a href="/" style="color: var(--text-light); text-decoration: none;">← 返回分类列表</a>
</div>
</div>
{% endblock %}
+92
View File
@@ -0,0 +1,92 @@
{% extends "base.html" %}
{% block title %}郭安的小站{% endblock %}
{% block content %}
<div style="margin-bottom: 2rem;">
<h2 style="
font-size: 1.1rem;
font-weight: 600;
color: var(--text);
margin-bottom: 1.5rem;
padding-bottom: 0.8rem;
border-bottom: 1px solid var(--border);
">分类</h2>
{% for category in categories %}
<div class="post-item">
<h2>
<a href="{{ category.get_absolute_url }}">{{ category.name }}</a>
</h2>
<div style="
font-size: 0.85rem;
color: var(--text-muted);
font-family: -apple-system, 'Noto Sans SC', sans-serif;
margin-top: 0.3rem;
">
{{ category.posts.count }} 篇文章
{% if category.description %} · {{ category.description }}{% endif %}
</div>
</div>
{% empty %}
<div class="post-item" style="text-align: center; padding: 4rem 0; color: var(--text-muted);">
<p style="margin-bottom: 0.5rem;">还没有分类</p>
<p style="font-size: 0.9rem;">
<a href="/admin/" style="color: var(--text-light); text-decoration: underline; text-decoration-color: var(--border); text-underline-offset: 3px;">去管理后台创建一个</a>
</p>
</div>
{% endfor %}
<!-- ✅ 分页 -->
{% if categories.paginator.num_pages > 1 %}
<nav style="
margin-top: 3rem;
text-align: center;
font-family: -apple-system, 'Noto Sans SC', sans-serif;
font-size: 0.9rem;
">
{% if categories.has_previous %}
<a href="?page=1" style="margin-right: 0.6rem; color: var(--text-light); text-decoration: none;">« 首页</a>
<a href="?page={{ categories.previous_page_number }}" style="margin-right: 1rem; color: var(--text-light); text-decoration: none;">‹ 上一页</a>
{% else %}
<span style="margin-right: 0.6rem; color: var(--text-muted);">« 首页</span>
<span style="margin-right: 1rem; color: var(--text-muted);">‹ 上一页</span>
{% endif %}
{% for num in categories.paginator.page_range %}
{% if categories.paginator.num_pages <= 9 %}
{% if num == categories.number %}
<span style="margin: 0 0.3rem; color: var(--bg); background: var(--text); padding: 0.2rem 0.6rem; border-radius: var(--radius); font-weight: 600;">{{ num }}</span>
{% else %}
<a href="?page={{ num }}" style="margin: 0 0.3rem; color: var(--text-light); text-decoration: none;">{{ num }}</a>
{% endif %}
{% else %}
{% if num == 1 or num == categories.paginator.num_pages %}
{% if num == categories.number %}
<span style="margin: 0 0.3rem; color: var(--bg); background: var(--text); padding: 0.2rem 0.6rem; border-radius: var(--radius); font-weight: 600;">{{ num }}</span>
{% else %}
<a href="?page={{ num }}" style="margin: 0 0.3rem; color: var(--text-light); text-decoration: none;">{{ num }}</a>
{% endif %}
{% elif num >= categories.number|add:'-2' and num <= categories.number|add:'2' %}
{% if num == categories.number %}
<span style="margin: 0 0.3rem; color: var(--bg); background: var(--text); padding: 0.2rem 0.6rem; border-radius: var(--radius); font-weight: 600;">{{ num }}</span>
{% else %}
<a href="?page={{ num }}" style="margin: 0 0.3rem; color: var(--text-light); text-decoration: none;">{{ num }}</a>
{% endif %}
{% elif num == categories.number|add:'-3' or num == categories.number|add:'3' %}
<span style="margin: 0 0.2rem; color: var(--text-muted);">…</span>
{% endif %}
{% endif %}
{% endfor %}
{% if categories.has_next %}
<a href="?page={{ categories.next_page_number }}" style="margin-left: 1rem; color: var(--text-light); text-decoration: none;">下一页 ›</a>
<a href="?page={{ categories.paginator.num_pages }}" style="margin-left: 0.6rem; color: var(--text-light); text-decoration: none;">末页 »</a>
{% else %}
<span style="margin-left: 1rem; color: var(--text-muted);">下一页 ›</span>
<span style="margin-left: 0.6rem; color: var(--text-muted);">末页 »</span>
{% endif %}
</nav>
{% endif %}
</div>
{% endblock %}
@@ -1,7 +1,7 @@
{% extends "base.html" %}
{% load static %}
{% block title %}{{ post.title }} - 郭安的小站{% endblock %}
{% block title %}{{ post.title }} - 人,是一根会思考的芦苇{% endblock %}
{% block content %}
<article>
+117
View File
@@ -0,0 +1,117 @@
{% extends "base.html" %}
{% load static %}
{% block title %}郭安的小站{% endblock %}
{% block content %}
{% for post in posts %}
<div class="post-item">
<h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<div class="post-date">{{ post.published_at|date:"Y年m月d日" }}</div>
{% if post.excerpt %}
<div class="post-summary">{{ post.excerpt }}</div>
{% endif %}
</div>
{% empty %}
<div class="post-item" style="text-align: center; padding: 4rem 0; color: var(--text-muted);">
<p style="margin-bottom: 0.5rem;">还没有文章</p>
<p style="font-size: 0.9rem;">
<a href="/admin/" style="color: var(--text-light); text-decoration: underline; text-decoration-color: var(--border); text-underline-offset: 3px;">去管理后台写一篇</a>
</p>
</div>
{% endfor %}
<!-- ✅ 分页组件(可点击页码 + 智能省略) -->
{% if posts.paginator.num_pages > 1 %}
<nav style="
margin-top: 3rem;
text-align: center;
font-family: -apple-system, 'Noto Sans SC', sans-serif;
font-size: 0.9rem;
">
<!-- 首页 -->
{% if posts.has_previous %}
<a href="?page=1" style="margin-right: 0.6rem; color: var(--text-light); text-decoration: none;">« 首页</a>
{% else %}
<span style="margin-right: 0.6rem; color: var(--text-muted);">« 首页</span>
{% endif %}
<!-- 上一页 -->
{% if posts.has_previous %}
<a href="?page={{ posts.previous_page_number }}" style="margin-right: 1rem; color: var(--text-light); text-decoration: none;">‹ 上一页</a>
{% else %}
<span style="margin-right: 1rem; color: var(--text-muted);">‹ 上一页</span>
{% endif %}
<!-- ✅ 页码列表(智能省略) -->
{% for num in posts.paginator.page_range %}
{% if posts.paginator.num_pages <= 9 %}
<!-- 总页数 ≤ 9:全部显示 -->
{% if num == posts.number %}
<span style="
margin: 0 0.3rem;
color: var(--bg);
background: var(--text);
padding: 0.2rem 0.6rem;
border-radius: var(--radius);
font-weight: 600;
">{{ num }}</span>
{% else %}
<a href="?page={{ num }}" style="margin: 0 0.3rem; color: var(--text-light); text-decoration: none;">{{ num }}</a>
{% endif %}
{% else %}
<!-- 总页数 > 9:智能省略 -->
{% if num == 1 or num == posts.paginator.num_pages %}
<!-- 第一页 & 最后一页始终显示 -->
{% if num == posts.number %}
<span style="
margin: 0 0.3rem;
color: var(--bg);
background: var(--text);
padding: 0.2rem 0.6rem;
border-radius: var(--radius);
font-weight: 600;
">{{ num }}</span>
{% else %}
<a href="?page={{ num }}" style="margin: 0 0.3rem; color: var(--text-light); text-decoration: none;">{{ num }}</a>
{% endif %}
{% elif num >= posts.number|add:'-2' and num <= posts.number|add:'2' %}
<!-- 当前页前后 2 页 -->
{% if num == posts.number %}
<span style="
margin: 0 0.3rem;
color: var(--bg);
background: var(--text);
padding: 0.2rem 0.6rem;
border-radius: var(--radius);
font-weight: 600;
">{{ num }}</span>
{% else %}
<a href="?page={{ num }}" style="margin: 0 0.3rem; color: var(--text-light); text-decoration: none;">{{ num }}</a>
{% endif %}
{% elif num == posts.number|add:'-3' or num == posts.number|add:'3' %}
<!-- 省略号 -->
<span style="margin: 0 0.2rem; color: var(--text-muted);">…</span>
{% endif %}
{% endif %}
{% endfor %}
<!-- 下一页 -->
{% if posts.has_next %}
<a href="?page={{ posts.next_page_number }}" style="margin-left: 1rem; color: var(--text-light); text-decoration: none;">下一页 ›</a>
{% else %}
<span style="margin-left: 1rem; color: var(--text-muted);">下一页 ›</span>
{% endif %}
<!-- 末页 -->
{% if posts.has_next %}
<a href="?page={{ posts.paginator.num_pages }}" style="margin-left: 0.6rem; color: var(--text-light); text-decoration: none;">末页 »</a>
{% else %}
<span style="margin-left: 0.6rem; color: var(--text-muted);">末页 »</span>
{% endif %}
</nav>
{% endif %}
{% endblock %}
+29
View File
@@ -0,0 +1,29 @@
{% extends "base.html" %}
{% block title %}{{ tag.name }} - 郭安的小站{% endblock %}
{% block content %}
<div style="margin-bottom: 2rem;">
<h2 style="font-size: 1.1rem; font-weight: 600; color: var(--text); margin-bottom: 1.5rem;">
🏷 {{ tag.name }}
</h2>
{% for post in posts %}
<div class="post-item">
<h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<div class="post-date">{{ post.published_at|date:"Y年m月d日" }}</div>
{% if post.excerpt %}
<div class="post-summary">{{ post.excerpt }}</div>
{% endif %}
</div>
{% empty %}
<div class="post-item" style="text-align: center; padding: 4rem 0; color: var(--text-muted);">
<p>这个标签下还没有文章</p>
</div>
{% endfor %}
<div style="margin-top: 2rem; padding-top: 1.5rem; border-top: 1px solid var(--border); font-size: 0.85rem; color: var(--text-muted);">
<a href="/" style="color: var(--text-light); text-decoration: none;">← 返回首页</a>
</div>
</div>
{% endblock %}
+12 -16
View File
@@ -1,23 +1,19 @@
"""
博客 app 的路由配置
把 URL 路径映射到视图函数
"""
from django.urls import path
# blog/urls.py
from django.urls import path, register_converter
from . import views
from .converters import UnicodeSlugConverter
register_converter(UnicodeSlugConverter, 'unislug')
# app_name 用于反向解析 URL
# 模板里用 {% url 'blog:post_list' %} 就会找到这里
app_name = 'blog'
urlpatterns = [
# 文章列表页
# URL: /
# name='post_list' 是反向解析的名字
path('', views.post_list, name='post_list'),
path('', views.category_list, name='category_list'),
# 文章详情页
# URL: /<slug>/
# <slug:slug> 表示这部分是 slug 类型,传给 views.post_detail 的 slug 参数
path('<slug:slug>/', views.post_detail, name='post_detail'),
# ✅ 用 unislug 替代 slug
path('category/<unislug:slug>/', views.category_detail, name='category_detail'),
path('post/<slug:slug>/', views.post_detail, name='post_detail'),
path('tag/<unislug:slug>/', views.tag_detail, name='tag_detail'),
path('about/', views.about_page, name='about'),
]
+48 -43
View File
@@ -1,54 +1,59 @@
"""
视图函数
每个函数对应一个页面
接收 HTTP 请求,返回 HTTP 响应
"""
from django.shortcuts import render, get_object_or_404
from .models import Post
from django.core.paginator import Paginator
from .models import Category, Post, Tag , SiteInfo
def post_list(request):
"""
文章列表页
def category_list(request):
"""首页:展示所有分类"""
category_list = Category.objects.all()
逻辑:
1. 从数据库查询所有已发布的文章
2. 按发布时间倒序排列(模型 Meta 里已定义 ordering)
3. 把数据传给模板渲染
"""
# filter(status='published') 只取已发布的
# 草稿不会出现在前台
posts = Post.objects.filter(status='published')
paginator = Paginator(category_list, 6) # 每页 10 个分类
page_number = request.GET.get('page')
categories = paginator.get_page(page_number)
# render 函数:加载模板 + 传入上下文数据 + 返回 HttpResponse
return render(
request, # HTTP 请求对象
'blog/post_list.html', # 模板路径
{'posts': posts} # 传给模板的数据(上下文)
)
return render(request, 'category_list.html', {
'categories': categories,
})
def category_detail(request, slug):
"""分类详情:该分类下的文章列表"""
category = get_object_or_404(Category, slug=slug)
post_list = category.posts.filter(status='published')
paginator = Paginator(post_list, 5)
page_number = request.GET.get('page')
posts = paginator.get_page(page_number)
return render(request, 'category_detail.html', {
'category': category,
'posts': posts,
})
def post_detail(request, slug):
"""
文章详情页
"""文章详情"""
post = get_object_or_404(Post, slug=slug, status='published')
return render(request, 'post_detail.html', {'post': post})
参数:
slug: URL 里的文章别名,比如 /hello-world/ 里的 hello-world
逻辑:
1. 根据 slug 查找文章
2. 找不到就返回 404
3. 找到就渲染详情模板
"""
# get_object_or_404:查到了返回对象,查不到直接抛 404
post = get_object_or_404(
Post, # 模型类
slug=slug, # 查找条件:slug 匹配
status='published' # 且状态是已发布
)
def tag_detail(request, slug):
"""标签详情:该标签下的文章列表"""
tag = get_object_or_404(Tag, slug=slug)
post_list = tag.posts.filter(status='published')
return render(
request,
'blog/post_detail.html',
{'post': post}
)
paginator = Paginator(post_list, 5)
page_number = request.GET.get('page')
posts = paginator.get_page(page_number)
return render(request, 'tag_detail.html', {
'tag': tag,
'posts': posts,
})
def about_page(request):
"""关于页面"""
site_info = SiteInfo.get_site_info()
return render(request, 'about.html', {
'site_info': site_info,
})