Merge feature/Category into develop

This commit is contained in:
2025-07-27 22:21:51 +08:00
7 changed files with 489 additions and 67 deletions

View File

@@ -1,5 +1,5 @@
from django.contrib import admin
from .models import Post
from .models import Post, Category
from django.db import models
from mdeditor.widgets import MDEditorWidget
@@ -18,3 +18,4 @@ class PostAdmin(admin.ModelAdmin):
# 注册自定义的PostAdmin
admin.site.register(Post, PostAdmin)
admin.site.register(Category)

View File

@@ -0,0 +1,25 @@
# Generated by Django 5.1 on 2025-07-27 13:26
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0005_remove_post_image'),
]
operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, unique=True)),
('description', models.TextField(blank=True, null=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
],
options={
'verbose_name_plural': 'Categories',
},
),
]

View File

@@ -0,0 +1,19 @@
# Generated by Django 5.1 on 2025-07-27 13:32
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0006_category'),
]
operations = [
migrations.AddField(
model_name='post',
name='category',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='posts', to='blog.category'),
),
]

View File

@@ -6,12 +6,27 @@ from django.utils.safestring import mark_safe
from mdeditor.fields import MDTextField
# 添加 Category 模型
class Category(models.Model):
name = models.CharField(max_length=100, unique=True)
description = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Categories"
class Post(models.Model):
title = models.CharField(max_length=100)
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')
def __str__(self):
return f"{self.title}"

View File

@@ -3,59 +3,199 @@
<head>
<meta charset="UTF-8">
<title>{{ post.title }}</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
display: flex;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.sidebar {
width: 250px;
padding-right: 20px;
border-right: 1px solid #eee;
}
.sidebar h3 {
color: #333;
border-bottom: 2px solid #007cba;
padding-bottom: 10px;
}
.sidebar ul {
list-style: none;
padding: 0;
}
.sidebar ul li {
margin: 10px 0;
}
.sidebar ul li a {
text-decoration: none;
color: #666;
display: block;
padding: 8px 12px;
border-radius: 4px;
transition: all 0.3s;
}
.sidebar ul li a:hover,
.sidebar ul li a.active {
background-color: #007cba;
color: white;
}
.search-box {
margin-bottom: 20px;
}
.search-box input[type="text"] {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.search-box input[type="submit"] {
width: 100%;
padding: 8px;
background-color: #007cba;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 5px;
}
.search-box input[type="submit"]:hover {
background-color: #005a87;
}
.search-type {
margin: 10px 0;
}
.search-type label {
display: block;
margin: 5px 0;
font-size: 14px;
color: #666;
}
.search-type input[type="radio"] {
margin-right: 5px;
}
.main-content {
flex: 1;
padding-left: 20px;
}
.main-content h1 {
color: #333;
border-bottom: 2px solid #eee;
padding-bottom: 10px;
}
.post-meta {
font-size: 14px;
color: #999;
margin-bottom: 20px;
}
.post-content img {
max-width: 100%;
height: auto;
display: block;
margin: 10px 0;
}
.post-content pre {
background-color: #f4f4f4;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
overflow-x: auto;
margin: 10px 0;
}
.post-content code {
font-family: 'Courier New', Courier, monospace;
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 3px;
font-size: 0.9em;
}
.post-content pre code {
background-color: transparent;
padding: 0;
border-radius: 0;
font-size: 0.9em;
}
footer {
position: fixed;
bottom: 0;
width: 100%;
text-align: center;
font-size: 12px;
color: #999;
background-color: white;
padding: 5px 0;
}
</style>
</head>
<body>
<!-- 添加样式使整个页面内容居中显示 -->
<div style="max-width: 800px; margin: 0 auto; padding: 0 20px;">
<!-- 添加网站标题 -->
<!-- 添加网站标题 -->
<div style="max-width: 1200px; margin: 0 auto; padding: 0 20px;">
<h1 style="text-align: left;">六桂流芳的com</h1>
<h1 style="text-align: center;">{{ post.title }}</h1>
<!-- 将返回首页链接放在发布时间的右侧 -->
<div style="display: flex; align-items: center; justify-content: space-between;">
<p>发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}</p>
<a href="{% url 'index' %}" style="margin-left: 20px; white-space: nowrap;">返回首页</a>
</div>
<!-- 添加CSS样式限制图片大小 -->
<div style="max-width: 100%;">
<style>
.post-content img {
max-width: 100%;
height: auto;
display: block;
margin: 10px 0;
}
.post-content pre {
background-color: #f4f4f4;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
overflow-x: auto;
margin: 10px 0;
}
.post-content code {
font-family: 'Courier New', Courier, monospace;
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 3px;
font-size: 0.9em;
}
.post-content pre code {
background-color: transparent;
padding: 0;
border-radius: 0;
font-size: 0.9em;
}
</style>
<div class="post-content">{{ post.get_markdown_content }}</div>
</div>
<br>
</div>
<footer style="position: fixed; bottom: 0; width: 100%; text-align: center; font-size: 12px; color: #999; background-color: white; padding: 5px 0;">
<div class="container">
<div class="sidebar">
<div class="search-box">
<form method="get" action="{% url 'index' %}">
<input type="text" name="q" placeholder="搜索文章..." value="{{ query|default:'' }}">
<div class="search-type">
<label>
<input type="radio" name="search_type" value="all" checked>
全文搜索
</label>
<label>
<input type="radio" name="search_type" value="title">
标题搜索
</label>
<label>
<input type="radio" name="search_type" value="content">
内容搜索
</label>
</div>
<input type="submit" value="搜索">
</form>
</div>
<h3>文章分类</h3>
<ul>
<li><a href="{% url 'index' %}" {% if not post.category %}class="active"{% endif %}>全部文章</a></li>
{% for category in categories %}
<li>
<a href="{% url 'index' %}?category={{ category.id }}"
{% if post.category and post.category.id == category.id %}class="active"{% endif %}>
{{ category.name }}
</a>
</li>
{% endfor %}
</ul>
</div>
<div class="main-content">
<h1 style="text-align: center;">{{ post.title }}</h1>
<div class="post-meta">
<div style="display: flex; justify-content: space-between; align-items: center;">
<span>发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}</span>
<a href="{% url 'index' %}" style="white-space: nowrap;">返回首页</a>
</div>
</div>
<div class="post-content">{{ post.get_markdown_content }}</div>
<br>
</div>
</div>
<footer>
<a href="https://beian.miit.gov.cn/" target="_blank">闽ICP备2023010767号-2</a>
</footer>
</body>
</html>

View File

@@ -3,22 +3,202 @@
<head>
<meta charset="UTF-8">
<title>六桂流芳的com</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
display: flex;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.sidebar {
width: 250px;
padding-right: 20px;
border-right: 1px solid #eee;
}
.sidebar h3 {
color: #333;
border-bottom: 2px solid #007cba;
padding-bottom: 10px;
}
.sidebar ul {
list-style: none;
padding: 0;
}
.sidebar ul li {
margin: 10px 0;
}
.sidebar ul li a {
text-decoration: none;
color: #666;
display: block;
padding: 8px 12px;
border-radius: 4px;
transition: all 0.3s;
}
.sidebar ul li a:hover,
.sidebar ul li a.active {
background-color: #007cba;
color: white;
}
.search-box {
margin-bottom: 20px;
}
.search-box input[type="text"] {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.search-box input[type="submit"] {
width: 100%;
padding: 8px;
background-color: #007cba;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 5px;
}
.search-box input[type="submit"]:hover {
background-color: #005a87;
}
.search-type {
margin: 10px 0;
}
.search-type label {
display: block;
margin: 5px 0;
font-size: 14px;
color: #666;
}
.search-type input[type="radio"] {
margin-right: 5px;
}
.main-content {
flex: 1;
padding-left: 20px;
}
.main-content h1 {
color: #333;
border-bottom: 2px solid #eee;
padding-bottom: 10px;
}
.posts-list {
list-style: none;
padding: 0;
}
.posts-list li {
padding: 15px 0;
border-bottom: 1px solid #eee;
}
.posts-list li:last-child {
border-bottom: none;
}
.post-title {
font-size: 18px;
font-weight: bold;
margin-bottom: 8px;
}
.post-title a {
text-decoration: none;
color: #007cba;
}
.post-meta {
font-size: 14px;
color: #999;
}
.post-category {
display: inline-block;
background-color: #f0f0f0;
padding: 2px 6px;
border-radius: 3px;
font-size: 12px;
margin-top: 5px;
}
.post-summary {
margin: 10px 0;
line-height: 1.6;
color: #666;
}
footer {
}
</style>
</head>
<body>
<!-- 修改样式使页面内容靠左对齐,标题显示在左上角 -->
<div style="max-width: 800px; margin: 0 auto; padding: 0 20px; text-align: left;">
<!-- 添加网站标题 -->
<div style="max-width: 1200px; margin: 0 auto; padding: 0 20px;">
<h1 style="text-align: left;">六桂流芳的com</h1>
<ul style="list-style: none; padding: 0;">
{% for post in posts %}
<li style="margin: 10px 0; text-align: left;">
<div style="display: flex; justify-content: space-between; align-items: center;">
<a href="{% url 'detail' post.id %}">{{ post.title }}</a>
<span style="margin-left: 10px; white-space: nowrap;">发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}</span>
</div>
</li>
{% endfor %}
</ul>
</div>
<div class="container">
<div class="sidebar">
<div class="search-box">
<form method="get">
<input type="text" name="q" placeholder="搜索文章..." value="{{ query|default:'' }}">
<div class="search-type">
<label>
<input type="radio" name="search_type" value="all" {% if search_type != 'title' and search_type != 'content' %}checked{% endif %}>
全文搜索
</label>
<label>
<input type="radio" name="search_type" value="title" {% if search_type == 'title' %}checked{% endif %}>
标题搜索
</label>
<label>
<input type="radio" name="search_type" value="content" {% if search_type == 'content' %}checked{% endif %}>
内容搜索
</label>
</div>
<input type="submit" value="搜索">
</form>
</div>
<h3>文章分类</h3>
<ul>
<li><a href="{% url 'index' %}" {% if not selected_category %}class="active"{% endif %}>全部文章</a></li>
{% for category in categories %}
<li>
<a href="?category={{ category.id }}"
{% if selected_category == category.id|stringformat:"s" %}class="active"{% endif %}>
{{ category.name }}
</a>
</li>
{% endfor %}
</ul>
</div>
<div class="main-content">
<ul class="posts-list">
{% for post in posts %}
<li>
<div class="post-title">
<a href="{% url 'detail' post.id %}">{{ post.title }}</a>
</div>
<div class="post-meta">
发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}
</div>
<div class="post-summary">
{{ post.summary|safe }}
</div>
{% if post.category %}
<div class="post-category">
{{ post.category.name }}
</div>
{% endif %}
</li>
{% endfor %}
</ul>
</div>
</div>
<footer style="position: fixed; bottom: 0; width: 100%; text-align: center; font-size: 12px; color: #999; background-color: white; padding: 5px 0;">
<a href="https://beian.miit.gov.cn/" target="_blank">闽ICP备2023010767号-2</a>
</footer>

View File

@@ -1,14 +1,56 @@
from django.shortcuts import render, get_object_or_404
from .models import Post
from .models import Post, Category
# Create your views here.
def index(request):
posts = Post.objects.order_by('created_at')
return render(request, 'blog/index.html', {'posts': posts})
# 获取所有分类
categories = Category.objects.all()
# 获取查询参数中的分类ID
category_id = request.GET.get('category')
# 获取搜索关键词
query = request.GET.get('q')
# 获取搜索类型参数
search_type = request.GET.get('search_type', 'all')
# 根据分类和搜索关键词筛选文章
if query:
# 根据搜索类型执行不同的搜索
if search_type == 'title':
posts = Post.objects.filter(title__icontains=query)
elif search_type == 'content':
posts = Post.objects.filter(content__icontains=query)
else:
# 默认按标题和内容搜索
posts = Post.objects.filter(title__icontains=query) | Post.objects.filter(content__icontains=query)
elif category_id:
posts = Post.objects.filter(category_id=category_id)
else:
posts = Post.objects.all()
posts = posts.order_by('-publish_date').distinct()
# 为每篇文章添加摘要前200个字符
for post in posts:
# 移除HTML标签并截取前200个字符作为摘要
import re
clean_content = re.sub(r'<[^>]+>', '', post.get_markdown_content())
post.summary = clean_content[:200] + '...' if len(clean_content) > 200 else clean_content
return render(request, 'blog/index.html', {
'posts': posts,
'categories': categories,
'selected_category': category_id,
'query': query,
'search_type': search_type
})
def detail(request, post_id):
post = get_object_or_404(Post, pk=post_id)
return render(request, 'blog/detail.html', {'post': post})
categories = Category.objects.all() # 获取所有分类用于侧边栏
return render(request, 'blog/detail.html', {'post': post, 'categories': categories})