Compare commits

..

4 Commits

Author SHA256 Message Date
5cf22beafa Merge feature/tempfile into develop 2025-08-01 14:39:52 +08:00
f0674c29da Add tempfile 2025-08-01 14:39:26 +08:00
c1918b45b3 Merge bugfix/init_code into develop 2025-07-28 02:04:25 +08:00
a9389042d9 Init All Code 2025-07-28 01:58:12 +08:00
9 changed files with 160 additions and 88 deletions

View File

@@ -0,0 +1,18 @@
# 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

@@ -36,13 +36,23 @@ class SiteSettings(models.Model):
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

@@ -290,28 +290,28 @@
<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

@@ -285,10 +285,11 @@
<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" target="_blank">{{ category.name }}分类</a> <a href="{% url 'category_feed' category.id %}" class="rss-feed-link"
<div class="rss-feed-description">{{ category.name }}分类下的最新博客文章</div> target="_blank">{{ category.name }}分类</a>
</li> <div class="rss-feed-description">{{ category.name }}分类下的最新博客文章</div>
</li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

View File

@@ -1,6 +1,48 @@
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.
@@ -29,22 +71,21 @@ def index(request):
if query: if query:
# 根据搜索类型执行不同的搜索 # 根据搜索类型执行不同的搜索
if search_type == 'title': if search_type == 'title':
posts = Post.objects.filter(title__icontains=query) posts = Post.objects.filter(title__icontains=query, status=Post.PUBLISHED)
elif search_type == 'content': elif search_type == 'content':
posts = Post.objects.filter(content__icontains=query) posts = Post.objects.filter(content__icontains=query, status=Post.PUBLISHED)
else: else:
# 默认按标题和内容搜索 # 默认按标题和内容搜索
posts = Post.objects.filter(title__icontains=query) | Post.objects.filter(content__icontains=query) posts = Post.objects.filter(title__icontains=query, status=Post.PUBLISHED) | Post.objects.filter(content__icontains=query, status=Post.PUBLISHED)
elif category_id: elif category_id:
posts = Post.objects.filter(category_id=category_id) posts = Post.objects.filter(category_id=category_id, status=Post.PUBLISHED)
else: else:
posts = Post.objects.all() posts = Post.objects.filter(status=Post.PUBLISHED)
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
@@ -60,13 +101,16 @@ def index(request):
def detail(request, post_id): def detail(request, post_id):
post = get_object_or_404(Post, pk=post_id) post = get_object_or_404(Post, pk=post_id, status=Post.PUBLISHED)
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', {'post': post, 'categories': categories, 'site_settings': site_settings}) return render(request, 'blog/detail.html', {
'post': post,
'categories': categories,
'site_settings': site_settings})
# 添加RSS页面视图 # 添加RSS页面视图

View File

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