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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,22 +1,64 @@
from django.shortcuts import render, get_object_or_404
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.
def index(request):
# 获取所有分类
categories = Category.objects.all()
# 获取查询参数中的分类ID
category_id = request.GET.get('category')
# 获取搜索关键词
query = request.GET.get('q')
# 获取搜索类型参数
search_type = request.GET.get('search_type', 'all')
# 获取站点设置,如果不存在则使用默认值
try:
site_settings = SiteSettings.objects.first()
@@ -24,31 +66,30 @@ def index(request):
except SiteSettings.DoesNotExist:
site_settings = None
summary_length = 50
# 根据分类和搜索关键词筛选文章
if query:
# 根据搜索类型执行不同的搜索
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':
posts = Post.objects.filter(content__icontains=query)
posts = Post.objects.filter(content__icontains=query, status=Post.PUBLISHED)
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:
posts = Post.objects.filter(category_id=category_id)
posts = Post.objects.filter(category_id=category_id, status=Post.PUBLISHED)
else:
posts = Post.objects.all()
posts = Post.objects.filter(status=Post.PUBLISHED)
posts = posts.order_by('-publish_date').distinct()
# 为每篇文章添加摘要(根据设置的字符长度)
for post in posts:
# 移除HTML标签并截取前N个字符作为摘要
import re
clean_content = re.sub(r'<[^>]+>', '', post.get_markdown_content())
post.summary = clean_content[:summary_length] + '...' if len(clean_content) > summary_length else clean_content
return render(request, 'blog/index.html', {
'posts': posts,
'categories': categories,
@@ -60,13 +101,16 @@ def index(request):
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() # 获取所有分类用于侧边栏
try:
site_settings = SiteSettings.objects.first()
except SiteSettings.DoesNotExist:
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页面视图
@@ -90,4 +134,4 @@ def contact_page(request):
site_settings = None
return render(request, 'blog/contact.html', {
'site_settings': site_settings
})
})

View File

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