Skip to content
This repository was archived by the owner on Oct 4, 2025. It is now read-only.

Commit 36ccaf9

Browse files
author
仿生猫梦见苦力怕
committed
Added comment function.
1 parent 1fb03d3 commit 36ccaf9

File tree

7 files changed

+211
-28
lines changed

7 files changed

+211
-28
lines changed

app/forms.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from flask_wtf import FlaskForm
2+
from wtforms import TextAreaField, SubmitField
3+
from wtforms.validators import DataRequired
4+
5+
6+
class CommentForm(FlaskForm):
7+
content = TextAreaField('评论内容', validators=[DataRequired()])
8+
submit = SubmitField('提交评论')
9+
10+
11+
class EditCommentForm(FlaskForm):
12+
content = TextAreaField('评论内容', validators=[DataRequired()])
13+
submit = SubmitField('更新评论')

app/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class User(SQLModel, table=True):
1010
password_hash: str
1111

1212
posts: List["Post"] = Relationship(back_populates="author")
13+
comments: List["Comment"] = Relationship(back_populates="author")
1314

1415
# Flask-Login 所需的属性和方法
1516
@property
@@ -36,4 +37,16 @@ class Post(SQLModel, table=True):
3637
created_at: datetime = Field(default_factory=datetime.utcnow) # 文章创建时间
3738

3839
author: User = Relationship(back_populates="posts")
40+
comments: List["Comment"] = Relationship(back_populates="post") # 博文的评论
3941

42+
43+
class Comment(SQLModel, table=True):
44+
id: Optional[int] = Field(default=None, primary_key=True)
45+
content: str # 评论内容
46+
created_at: datetime = Field(default_factory=datetime.utcnow) # 评论时间
47+
author_id: int = Field(foreign_key="user.id") # 评论作者
48+
post_id: int = Field(foreign_key="post.id") # 所属博文
49+
50+
# 关系
51+
author: "User" = Relationship(back_populates="comments")
52+
post: "Post" = Relationship(back_populates="comments")

app/routes.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from datetime import datetime
66

77
from app import engine
8-
from app.models import Post, User
8+
from app.models import Post, User, Comment
9+
from app.forms import CommentForm, EditCommentForm
910

1011
# 创建蓝图
1112
main = Blueprint('main', __name__)
@@ -66,12 +67,29 @@ def view_post(post_id):
6667
with Session(engine) as session:
6768
post = session.get(Post, post_id)
6869
if post:
69-
return render_template('view_post.html', post=post)
70+
return render_template('view_post.html', post=post, form=CommentForm())
7071
else:
7172
flash('博文未找到!')
7273
return redirect(url_for('main.index'))
7374

7475

76+
@main.route('/post/<int:post_id>/comment', methods=['POST'])
77+
@login_required
78+
def add_comment(post_id):
79+
form = CommentForm()
80+
if form.validate_on_submit():
81+
with Session(engine) as session:
82+
new_comment = Comment(
83+
content=form.content.data,
84+
author_id=current_user.id,
85+
post_id=post_id
86+
)
87+
session.add(new_comment)
88+
session.commit()
89+
flash('评论已发布!', 'success')
90+
return redirect(url_for('main.view_post', post_id=post_id))
91+
92+
7593
@main.route('/edit_post/<int:post_id>', methods=['GET', 'POST'])
7694
@login_required
7795
def edit_post(post_id):
@@ -116,3 +134,47 @@ def delete_post(post_id):
116134
session.commit()
117135
flash('你的博文已被删除!')
118136
return redirect(url_for('main.dashboard'))
137+
138+
139+
@main.route('/comment/<int:comment_id>/edit', methods=['GET', 'POST'])
140+
@login_required
141+
def edit_comment(comment_id):
142+
with Session(engine) as session:
143+
comment = session.get(Comment, comment_id)
144+
if not comment:
145+
flash('评论未找到!', 'error')
146+
return redirect(url_for('main.index'))
147+
if comment.author_id != current_user.id:
148+
flash('你没有权限编辑这条评论!', 'error')
149+
return redirect(url_for('main.view_post', post_id=comment.post_id))
150+
151+
form = EditCommentForm()
152+
if form.validate_on_submit():
153+
comment.content = form.content.data
154+
session.commit()
155+
flash('评论已更新!', 'success')
156+
return redirect(url_for('main.view_post', post_id=comment.post_id))
157+
elif request.method == 'GET':
158+
form.content.data = comment.content
159+
160+
return render_template('edit_comment.html', form=form, comment=comment)
161+
162+
163+
# 删除评论
164+
@main.route('/comment/<int:comment_id>/delete', methods=['POST'])
165+
@login_required
166+
def delete_comment(comment_id):
167+
with Session(engine) as session:
168+
comment = session.get(Comment, comment_id)
169+
if not comment:
170+
flash('评论未找到!', 'error')
171+
return redirect(url_for('main.index'))
172+
if comment.author_id != current_user.id:
173+
flash('你没有权限删除这条评论!', 'error')
174+
return redirect(url_for('main.view_post', post_id=comment.post_id))
175+
176+
post_id = comment.post_id
177+
session.delete(comment)
178+
session.commit()
179+
flash('评论已删除!', 'success')
180+
return redirect(url_for('main.view_post', post_id=post_id))

app/static/css/styles.css

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -196,32 +196,7 @@ footer {
196196
transition: background-color 0.3s ease;
197197
}
198198

199-
/* 返回按钮 */
200-
.back-button {
201-
display: inline-block;
202-
padding: 10px 20px;
203-
background-color: #6c757d;
204-
color: white;
205-
border: none;
206-
border-radius: 5px;
207-
font-size: 16px;
208-
cursor: pointer;
209-
transition: background-color 0.3s ease;
210-
text-decoration: none;
211-
margin-top: 20px;
212-
}
213199

214-
.back-button:hover {
215-
background-color: #5a6268;
216-
}
217-
218-
.form-button:hover {
219-
background-color: #0056b3;
220-
}
221-
222-
.back-link:hover {
223-
text-decoration: underline;
224-
}
225200

226201
/* 文章容器 */
227202
.post-container {
@@ -306,4 +281,64 @@ footer {
306281

307282
.back-link:hover {
308283
text-decoration: underline;
284+
}
285+
286+
/* 评论表单 */
287+
.comment-form {
288+
margin-top: 40px;
289+
padding: 20px;
290+
background-color: #f9f9f9;
291+
border-radius: 10px;
292+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
293+
}
294+
295+
.comment-form h3 {
296+
margin-bottom: 20px;
297+
font-size: 1.2rem;
298+
color: #333;
299+
}
300+
301+
/* 评论列表 */
302+
.comment-list {
303+
margin-top: 40px;
304+
}
305+
306+
.comment-list h3 {
307+
margin-bottom: 20px;
308+
font-size: 1.2rem;
309+
color: #333;
310+
}
311+
312+
.comment-item {
313+
margin-bottom: 20px;
314+
padding: 15px;
315+
background-color: #ffffff;
316+
border-radius: 10px;
317+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
318+
}
319+
320+
.comment-meta {
321+
margin-bottom: 10px;
322+
font-size: 0.9rem;
323+
color: #666;
324+
}
325+
326+
.comment-meta .comment-author {
327+
font-weight: bold;
328+
margin-right: 10px;
329+
}
330+
331+
.comment-content {
332+
font-size: 1rem;
333+
line-height: 1.6;
334+
color: #444;
335+
}
336+
337+
/* 评论操作按钮 */
338+
.comment-actions {
339+
margin-top: 10px;
340+
}
341+
342+
.comment-actions .action-button {
343+
margin-right: 10px;
309344
}

app/templates/edit_comment.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends "base.html" %}
2+
3+
{% block title %}编辑评论 - 六(5)班 班级博客{% endblock %}
4+
5+
{% block content %}
6+
<h1>编辑评论</h1>
7+
<form method="POST" action="{{ url_for('main.edit_comment', comment_id=comment.id) }}" class="comment-form">
8+
{{ form.hidden_tag() }}
9+
<div class="form-group">
10+
{{ form.content(class="form-input", rows=4, placeholder="请输入你的评论...") }}
11+
</div>
12+
<button type="submit" class="form-button">更新评论</button>
13+
<button type="button" onclick="window.location.href='{{ url_for('main.view_post', post_id=comment.post_id) }}'" class="form-button back-button">取消</button>
14+
</form>
15+
{% endblock %}

app/templates/view_post.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,48 @@ <h1 class="post-title">{{ post.title }}</h1>
2222
{% endif %}
2323
<button onclick="window.location.href='{{ url_for('main.index') }}';" class="form-button back-button">返回首页</button>
2424
</div>
25+
26+
<!-- 评论表单 -->
27+
<div class="comment-form">
28+
<h3>发表评论</h3>
29+
<form method="POST" action="{{ url_for('main.add_comment', post_id=post.id) }}">
30+
{{ form.hidden_tag() }}
31+
<div class="form-group">
32+
{{ form.content(class="form-input", rows=4, placeholder="请输入你的评论...") }}
33+
</div>
34+
<button type="submit" class="form-button">提交评论</button>
35+
</form>
36+
</div>
37+
38+
<!-- 评论列表 -->
39+
<div class="comment-list" style="margin-bottom:80px;">
40+
<h3>评论</h3>
41+
{% if post.comments %}
42+
<ul>
43+
{% for comment in post.comments %}
44+
<li class="comment-item">
45+
<div class="comment-meta">
46+
<span class="comment-author">{{ comment.author.username }}</span>
47+
<span class="comment-date">{{ comment.created_at }}</span>
48+
</div>
49+
<div class="comment-content">
50+
{{ comment.content }}
51+
</div>
52+
{% if current_user == comment.author %}
53+
<div class="comment-actions">
54+
<button onclick="window.location.href='{{ url_for('main.edit_comment', comment_id=comment.id) }}'" class="action-button edit-button">编辑</button>
55+
<form action="{{ url_for('main.delete_comment', comment_id=comment.id) }}" method="POST" style="display:inline;">
56+
<button type="submit" class="action-button delete-button" onclick="return confirm('确定要删除这条评论吗?')">删除</button>
57+
</form>
58+
</div>
59+
{% endif %}
60+
</li>
61+
{% endfor %}
62+
</ul>
63+
{% else %}
64+
<p>还没有人发表评论,快来抢沙发!</p>
65+
{% endif %}
66+
</div>
67+
</div>
68+
2569
{% endblock %}

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ SQLModel==0.0.22
33
Flask-Login==0.5.0
44
Flask-WTF==0.15.1
55
Werkzeug~=2.0.3
6-
SQLAlchemy~=2.0.38
6+
SQLAlchemy~=2.0.38
7+
WTForms~=3.0.1

0 commit comments

Comments
 (0)