Compare commits

..

11 Commits

Author SHA256 Message Date
dd3a044b7c Merge develop into main 2025-07-28 00:54:43 +08:00
fb6e2df45d Merge develop into main 2025-07-27 23:28:43 +08:00
200373e42f Merge develop into main 2025-07-27 23:23:24 +08:00
6ed045e292 Merge develop into main 2025-07-27 23:15:02 +08:00
742eec3a4d Merge develop into main 2025-07-27 18:24:30 +08:00
4d0d83bb0f Merge develop into main 2025-07-27 18:21:12 +08:00
3f85ceb0e8 Merge develop into main 2025-07-27 17:27:46 +08:00
2b571987be Merge develop into main 2025-07-27 15:05:40 +08:00
ee406be52b Merge develop into main 2025-07-26 23:45:28 +08:00
5c1f5f81d3 Merge develop into main 2025-07-26 20:38:24 +08:00
84e649081a Merge develop into main 2025-07-26 18:10:07 +08:00
9 changed files with 88 additions and 160 deletions

View File

@@ -1,18 +0,0 @@
# Generated by Django 5.1 on 2025-07-31 15:06
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0009_sitesettings_contact_email_and_more'),
]
operations = [
migrations.AddField(
model_name='post',
name='status',
field=models.CharField(choices=[('draft', '草稿'), ('published', '已发布')], default='draft', max_length=10),
),
]

View File

@@ -30,29 +30,19 @@ class SiteSettings(models.Model):
class Meta: class Meta:
verbose_name = "站点设置" verbose_name = "站点设置"
verbose_name_plural = "站点设置" verbose_name_plural = "站点设置"
def __str__(self): def __str__(self):
return "站点设置" return "站点设置"
class Post(models.Model): class Post(models.Model):
# 添加状态选项
DRAFT = 'draft'
PUBLISHED = 'published'
STATUS_CHOICES = [
(DRAFT, '草稿'),
(PUBLISHED, '已发布'),
]
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
content = MDTextField() content = MDTextField() # ✅ 改成这里
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True) updated_at = models.DateTimeField(auto_now=True)
publish_date = models.DateTimeField(default=timezone.now) publish_date = models.DateTimeField(default=timezone.now)
# 添加分类字段,建立外键关系 # 添加分类字段,建立外键关系
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True, related_name='posts') category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True, related_name='posts')
# 添加状态字段
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default=DRAFT)
def __str__(self): def __str__(self):
return f"{self.title}" return f"{self.title}"

View File

@@ -122,7 +122,7 @@
background-color: white; background-color: white;
padding: 5px 0; padding: 5px 0;
} }
.contact-info { .contact-info {
background-color: #f9f9f9; background-color: #f9f9f9;
padding: 15px; padding: 15px;
@@ -130,23 +130,23 @@
margin-top: 20px; margin-top: 20px;
border: 1px solid #eee; border: 1px solid #eee;
} }
.contact-info h3 { .contact-info h3 {
margin-top: 0; margin-top: 0;
color: #333; color: #333;
border-bottom: 2px solid #007cba; border-bottom: 2px solid #007cba;
padding-bottom: 10px; padding-bottom: 10px;
} }
.contact-info ul { .contact-info ul {
padding-left: 20px; padding-left: 20px;
} }
.contact-info li { .contact-info li {
margin: 10px 0; margin: 10px 0;
color: #666; color: #666;
} }
/* 添加联系我主内容区域样式 */ /* 添加联系我主内容区域样式 */
.contact-main { .contact-main {
background-color: #f9f9f9; background-color: #f9f9f9;
@@ -154,68 +154,68 @@
border-radius: 5px; border-radius: 5px;
border: 1px solid #eee; border: 1px solid #eee;
} }
.contact-main h2 { .contact-main h2 {
color: #333; color: #333;
border-bottom: 2px solid #007cba; border-bottom: 2px solid #007cba;
padding-bottom: 10px; padding-bottom: 10px;
} }
.contact-description { .contact-description {
margin: 15px 0; margin: 15px 0;
line-height: 1.6; line-height: 1.6;
color: #666; color: #666;
} }
.contact-details { .contact-details {
list-style: none; list-style: none;
padding: 0; padding: 0;
} }
.contact-details li { .contact-details li {
padding: 10px 0; padding: 10px 0;
border-bottom: 1px solid #eee; border-bottom: 1px solid #eee;
} }
.contact-details li:last-child { .contact-details li:last-child {
border-bottom: none; border-bottom: none;
} }
.contact-label { .contact-label {
font-weight: bold; font-weight: bold;
color: #333; color: #333;
display: inline-block; display: inline-block;
width: 80px; width: 80px;
} }
.contact-value { .contact-value {
color: #666; color: #666;
} }
/* 添加导航栏样式 */ /* 添加导航栏样式 */
.top-nav { .top-nav {
background-color: #007cba; background-color: #007cba;
padding: 10px 0; padding: 10px 0;
margin-bottom: 20px; margin-bottom: 20px;
} }
.nav-container { .nav-container {
max-width: 1200px; max-width: 1200px;
margin: 0 auto; margin: 0 auto;
padding: 0 20px; padding: 0 20px;
} }
.nav-links { .nav-links {
list-style: none; list-style: none;
padding: 0; padding: 0;
margin: 0; margin: 0;
display: flex; display: flex;
} }
.nav-links li { .nav-links li {
margin-right: 20px; margin-right: 20px;
} }
.nav-links li a { .nav-links li a {
color: white; color: white;
text-decoration: none; text-decoration: none;
@@ -223,7 +223,7 @@
border-radius: 4px; border-radius: 4px;
transition: background-color 0.3s; transition: background-color 0.3s;
} }
.nav-links li a:hover { .nav-links li a:hover {
background-color: rgba(255, 255, 255, 0.2); background-color: rgba(255, 255, 255, 0.2);
} }
@@ -287,31 +287,31 @@
<div class="contact-description"> <div class="contact-description">
如果您有任何问题或想与我交流,可以通过以下方式联系我: 如果您有任何问题或想与我交流,可以通过以下方式联系我:
</div> </div>
<ul class="contact-details"> <ul class="contact-details">
{% if site_settings.contact_email %} {% if site_settings.contact_email %}
<li> <li>
<span class="contact-label">邮箱:</span> <span class="contact-label">邮箱:</span>
<span class="contact-value">{{ site_settings.contact_email }}</span> <span class="contact-value">{{ site_settings.contact_email }}</span>
</li> </li>
{% endif %} {% endif %}
{% if site_settings.contact_wechat %} {% if site_settings.contact_wechat %}
<li> <li>
<span class="contact-label">微信:</span> <span class="contact-label">微信:</span>
<span class="contact-value">{{ site_settings.contact_wechat }}</span> <span class="contact-value">{{ site_settings.contact_wechat }}</span>
</li> </li>
{% endif %} {% endif %}
{% if site_settings.contact_linkedin %} {% if site_settings.contact_linkedin %}
<li> <li>
<span class="contact-label">LinkedIn:</span> <span class="contact-label">LinkedIn:</span>
<span class="contact-value">{{ site_settings.contact_linkedin }}</span> <span class="contact-value">{{ site_settings.contact_linkedin }}</span>
</li> </li>
{% endif %} {% endif %}
{% if site_settings.contact_github %} {% if site_settings.contact_github %}
<li> <li>
<span class="contact-label">GitHub:</span> <span class="contact-label">GitHub:</span>
<span class="contact-value">{{ site_settings.contact_github }}</span> <span class="contact-value">{{ site_settings.contact_github }}</span>
</li> </li>
{% endif %} {% endif %}
</ul> </ul>
</div> </div>

View File

@@ -159,31 +159,31 @@
background-color: white; background-color: white;
padding: 5px 0; padding: 5px 0;
} }
/* 添加导航栏样式 */ /* 添加导航栏样式 */
.top-nav { .top-nav {
background-color: #007cba; background-color: #007cba;
padding: 10px 0; padding: 10px 0;
margin-bottom: 20px; margin-bottom: 20px;
} }
.nav-container { .nav-container {
max-width: 1200px; max-width: 1200px;
margin: 0 auto; margin: 0 auto;
padding: 0 20px; padding: 0 20px;
} }
.nav-links { .nav-links {
list-style: none; list-style: none;
padding: 0; padding: 0;
margin: 0; margin: 0;
display: flex; display: flex;
} }
.nav-links li { .nav-links li {
margin-right: 20px; margin-right: 20px;
} }
.nav-links li a { .nav-links li a {
color: white; color: white;
text-decoration: none; text-decoration: none;
@@ -191,7 +191,7 @@
border-radius: 4px; border-radius: 4px;
transition: background-color 0.3s; transition: background-color 0.3s;
} }
.nav-links li a:hover { .nav-links li a:hover {
background-color: rgba(255, 255, 255, 0.2); background-color: rgba(255, 255, 255, 0.2);
} }

View File

@@ -159,31 +159,31 @@
footer { footer {
} }
/* 添加导航栏样式 */ /* 添加导航栏样式 */
.top-nav { .top-nav {
background-color: #007cba; background-color: #007cba;
padding: 10px 0; padding: 10px 0;
margin-bottom: 20px; margin-bottom: 20px;
} }
.nav-container { .nav-container {
max-width: 1200px; max-width: 1200px;
margin: 0 auto; margin: 0 auto;
padding: 0 20px; padding: 0 20px;
} }
.nav-links { .nav-links {
list-style: none; list-style: none;
padding: 0; padding: 0;
margin: 0; margin: 0;
display: flex; display: flex;
} }
.nav-links li { .nav-links li {
margin-right: 20px; margin-right: 20px;
} }
.nav-links li a { .nav-links li a {
color: white; color: white;
text-decoration: none; text-decoration: none;
@@ -191,7 +191,7 @@
border-radius: 4px; border-radius: 4px;
transition: background-color 0.3s; transition: background-color 0.3s;
} }
.nav-links li a:hover { .nav-links li a:hover {
background-color: rgba(255, 255, 255, 0.2); background-color: rgba(255, 255, 255, 0.2);
} }

View File

@@ -122,31 +122,31 @@
background-color: white; background-color: white;
padding: 5px 0; padding: 5px 0;
} }
/* 添加导航栏样式 */ /* 添加导航栏样式 */
.top-nav { .top-nav {
background-color: #007cba; background-color: #007cba;
padding: 10px 0; padding: 10px 0;
margin-bottom: 20px; margin-bottom: 20px;
} }
.nav-container { .nav-container {
max-width: 1200px; max-width: 1200px;
margin: 0 auto; margin: 0 auto;
padding: 0 20px; padding: 0 20px;
} }
.nav-links { .nav-links {
list-style: none; list-style: none;
padding: 0; padding: 0;
margin: 0; margin: 0;
display: flex; display: flex;
} }
.nav-links li { .nav-links li {
margin-right: 20px; margin-right: 20px;
} }
.nav-links li a { .nav-links li a {
color: white; color: white;
text-decoration: none; text-decoration: none;
@@ -154,11 +154,11 @@
border-radius: 4px; border-radius: 4px;
transition: background-color 0.3s; transition: background-color 0.3s;
} }
.nav-links li a:hover { .nav-links li a:hover {
background-color: rgba(255, 255, 255, 0.2); background-color: rgba(255, 255, 255, 0.2);
} }
/* RSS主内容区域样式 */ /* RSS主内容区域样式 */
.rss-main { .rss-main {
background-color: #f9f9f9; background-color: #f9f9f9;
@@ -166,44 +166,44 @@
border-radius: 5px; border-radius: 5px;
border: 1px solid #eee; border: 1px solid #eee;
} }
.rss-main h2 { .rss-main h2 {
color: #333; color: #333;
border-bottom: 2px solid #ff6600; border-bottom: 2px solid #ff6600;
padding-bottom: 10px; padding-bottom: 10px;
} }
.rss-description { .rss-description {
margin: 15px 0; margin: 15px 0;
line-height: 1.6; line-height: 1.6;
color: #666; color: #666;
} }
.rss-feed-list { .rss-feed-list {
list-style: none; list-style: none;
padding: 0; padding: 0;
} }
.rss-feed-list li { .rss-feed-list li {
padding: 10px 0; padding: 10px 0;
border-bottom: 1px solid #eee; border-bottom: 1px solid #eee;
} }
.rss-feed-list li:last-child { .rss-feed-list li:last-child {
border-bottom: none; border-bottom: none;
} }
.rss-feed-link { .rss-feed-link {
font-size: 16px; font-size: 16px;
font-weight: bold; font-weight: bold;
color: #007cba; color: #007cba;
text-decoration: none; text-decoration: none;
} }
.rss-feed-link:hover { .rss-feed-link:hover {
text-decoration: underline; text-decoration: underline;
} }
.rss-feed-description { .rss-feed-description {
font-size: 14px; font-size: 14px;
color: #999; color: #999;
@@ -270,7 +270,7 @@
RSS是一种用于发布经常更新的内容的网页格式。通过RSS阅读器您可以订阅我们的内容及时获取最新文章更新。 RSS是一种用于发布经常更新的内容的网页格式。通过RSS阅读器您可以订阅我们的内容及时获取最新文章更新。
点击下面的链接可以在浏览器中查看RSS内容使用RSS阅读器订阅时请复制链接地址。 点击下面的链接可以在浏览器中查看RSS内容使用RSS阅读器订阅时请复制链接地址。
</div> </div>
<ul class="rss-feed-list"> <ul class="rss-feed-list">
<li> <li>
<a href="{% url 'rss_feed' %}" class="rss-feed-link" target="_blank">最新文章</a> <a href="{% url 'rss_feed' %}" class="rss-feed-link" target="_blank">最新文章</a>
@@ -285,11 +285,10 @@
<div class="rss-feed-description">包含所有博客文章</div> <div class="rss-feed-description">包含所有博客文章</div>
</li> </li>
{% for category in categories %} {% for category in categories %}
<li> <li>
<a href="{% url 'category_feed' category.id %}" class="rss-feed-link" <a href="{% url 'category_feed' category.id %}" class="rss-feed-link" target="_blank">{{ category.name }}分类</a>
target="_blank">{{ category.name }}分类</a> <div class="rss-feed-description">{{ category.name }}分类下的最新博客文章</div>
<div class="rss-feed-description">{{ category.name }}分类下的最新博客文章</div> </li>
</li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

View File

@@ -13,4 +13,4 @@ urlpatterns = [
path('rss/category/<int:category_id>/', CategoryPostsFeed(), name='category_feed'), path('rss/category/<int:category_id>/', CategoryPostsFeed(), name='category_feed'),
path('rss/recent/', RecentPostsFeed(), name='recent_feed'), path('rss/recent/', RecentPostsFeed(), name='recent_feed'),
path('rss/all/', AllPostsFeed(), name='all_feed'), path('rss/all/', AllPostsFeed(), name='all_feed'),
] ]

View File

@@ -1,64 +1,22 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from .models import Post, Category, SiteSettings from .models import Post, Category, SiteSettings
def post_list(request):
# 只显示已发布的文章
posts = Post.objects.filter(status=Post.PUBLISHED).order_by('-publish_date')
categories = Category.objects.all()
# 获取站点设置
site_settings = SiteSettings.objects.first()
summary_length = site_settings.summary_length if site_settings else 50
return render(request, 'blog/post_list.html', {
'posts': posts,
'categories': categories,
'summary_length': summary_length
})
def post_detail(request, pk):
# 只允许查看已发布的文章
post = get_object_or_404(Post, pk=pk, status=Post.PUBLISHED)
categories = Category.objects.all()
return render(request, 'blog/post_detail.html', {
'post': post,
'categories': categories
})
def category_posts(request, category_id):
category = get_object_or_404(Category, id=category_id)
# 只显示该分类下已发布的文章
posts = Post.objects.filter(category=category, status=Post.PUBLISHED).order_by('-publish_date')
categories = Category.objects.all()
# 获取站点设置
site_settings = SiteSettings.objects.first()
summary_length = site_settings.summary_length if site_settings else 50
return render(request, 'blog/post_list.html', {
'posts': posts,
'categories': categories,
'current_category': category,
'summary_length': summary_length
})
# Create your views here. # Create your views here.
def index(request): def index(request):
# 获取所有分类 # 获取所有分类
categories = Category.objects.all() categories = Category.objects.all()
# 获取查询参数中的分类ID # 获取查询参数中的分类ID
category_id = request.GET.get('category') category_id = request.GET.get('category')
# 获取搜索关键词 # 获取搜索关键词
query = request.GET.get('q') query = request.GET.get('q')
# 获取搜索类型参数 # 获取搜索类型参数
search_type = request.GET.get('search_type', 'all') search_type = request.GET.get('search_type', 'all')
# 获取站点设置,如果不存在则使用默认值 # 获取站点设置,如果不存在则使用默认值
try: try:
site_settings = SiteSettings.objects.first() site_settings = SiteSettings.objects.first()
@@ -66,30 +24,31 @@ def index(request):
except SiteSettings.DoesNotExist: except SiteSettings.DoesNotExist:
site_settings = None site_settings = None
summary_length = 50 summary_length = 50
# 根据分类和搜索关键词筛选文章 # 根据分类和搜索关键词筛选文章
if query: if query:
# 根据搜索类型执行不同的搜索 # 根据搜索类型执行不同的搜索
if search_type == 'title': if search_type == 'title':
posts = Post.objects.filter(title__icontains=query, status=Post.PUBLISHED) posts = Post.objects.filter(title__icontains=query)
elif search_type == 'content': elif search_type == 'content':
posts = Post.objects.filter(content__icontains=query, status=Post.PUBLISHED) posts = Post.objects.filter(content__icontains=query)
else: else:
# 默认按标题和内容搜索 # 默认按标题和内容搜索
posts = Post.objects.filter(title__icontains=query, status=Post.PUBLISHED) | Post.objects.filter(content__icontains=query, status=Post.PUBLISHED) posts = Post.objects.filter(title__icontains=query) | Post.objects.filter(content__icontains=query)
elif category_id: elif category_id:
posts = Post.objects.filter(category_id=category_id, status=Post.PUBLISHED) posts = Post.objects.filter(category_id=category_id)
else: else:
posts = Post.objects.filter(status=Post.PUBLISHED) posts = Post.objects.all()
posts = posts.order_by('-publish_date').distinct() posts = posts.order_by('-publish_date').distinct()
# 为每篇文章添加摘要(根据设置的字符长度) # 为每篇文章添加摘要(根据设置的字符长度)
for post in posts: for post in posts:
# 移除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[:summary_length] + '...' if len(clean_content) > summary_length 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,
'categories': categories, 'categories': categories,
@@ -101,16 +60,13 @@ def index(request):
def detail(request, post_id): def detail(request, post_id):
post = get_object_or_404(Post, pk=post_id, status=Post.PUBLISHED) post = get_object_or_404(Post, pk=post_id)
categories = Category.objects.all() # 获取所有分类用于侧边栏 categories = Category.objects.all() # 获取所有分类用于侧边栏
try: try:
site_settings = SiteSettings.objects.first() site_settings = SiteSettings.objects.first()
except SiteSettings.DoesNotExist: except SiteSettings.DoesNotExist:
site_settings = None site_settings = None
return render(request, 'blog/detail.html', { return render(request, 'blog/detail.html', {'post': post, 'categories': categories, 'site_settings': site_settings})
'post': post,
'categories': categories,
'site_settings': site_settings})
# 添加RSS页面视图 # 添加RSS页面视图
@@ -134,4 +90,4 @@ def contact_page(request):
site_settings = None site_settings = None
return render(request, 'blog/contact.html', { return render(request, 'blog/contact.html', {
'site_settings': site_settings 'site_settings': site_settings
}) })

View File

@@ -163,3 +163,4 @@ try:
from .local_settings import * from .local_settings import *
except ImportError: except ImportError:
pass pass