1
+ import markdown
1
2
from flask import Blueprint , render_template , request , url_for , flash
2
3
from flask_login import login_required , current_user
3
4
from sqlmodel import Session , select , desc
6
7
7
8
from app import engine
8
9
from app .models import Post , User , Comment
9
- from app .forms import CommentForm , EditCommentForm
10
+ from app .forms import CommentForm , EditCommentForm , PostForm
10
11
11
12
# 创建蓝图
12
13
main = Blueprint ('main' , __name__ )
@@ -44,22 +45,21 @@ def dashboard():
44
45
@main .route ('/create_post' , methods = ['GET' , 'POST' ])
45
46
@login_required
46
47
def create_post ():
47
- if request . method == 'POST' :
48
- title = request . form .get ( 'title' )
49
- content = request . form .get ( ' content' )
50
-
51
- if not title or not content :
52
- flash ( '你必须填写标题和内容字段!' )
53
- else :
54
- new_post = Post ( title = title , content = content , author_id = current_user .id )
55
- with Session ( engine ) as session :
56
- session . add ( new_post )
57
- session .commit ( )
58
- print ( f"New post created: { new_post } " ) # 调试信息
59
- flash ('你成功创建了一篇新的博文! ' )
48
+ form = PostForm ()
49
+ if form .validate_on_submit ():
50
+ html_content = markdown . markdown ( form .content . data ) # 解析 Markdown
51
+ new_post = Post (
52
+ title = form . title . data ,
53
+ content = form . content . data ,
54
+ html_content = html_content ,
55
+ author_id = current_user .id
56
+ )
57
+ with Session ( engine ) as session :
58
+ session .add ( new_post )
59
+ session . commit ()
60
+ flash ('博文已发布!' , 'success ' )
60
61
return redirect (url_for ('main.dashboard' ))
61
-
62
- return render_template ('create_post.html' )
62
+ return render_template ('create_post.html' , form = form )
63
63
64
64
65
65
@main .route ('/post/<int:post_id>' )
@@ -78,12 +78,14 @@ def view_post(post_id):
78
78
def add_comment (post_id ):
79
79
form = CommentForm ()
80
80
if form .validate_on_submit ():
81
+ html_content = markdown .markdown (form .content .data ) # 解析 Markdown
82
+ new_comment = Comment (
83
+ content = form .content .data ,
84
+ html_content = html_content ,
85
+ author_id = current_user .id ,
86
+ post_id = post_id
87
+ )
81
88
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
89
session .add (new_comment )
88
90
session .commit ()
89
91
flash ('评论已发布!' , 'success' )
@@ -111,11 +113,12 @@ def edit_post(post_id):
111
113
else :
112
114
post .title = title
113
115
post .content = content
116
+ post .html_content = markdown .markdown (content )
114
117
session .commit ()
115
118
flash ('你的博文已被更新!' )
116
119
return redirect (url_for ('main.view_post' , post_id = post .id ))
117
120
118
- return render_template ('edit_post.html' , post = post )
121
+ return render_template ('edit_post.html' , post = post , form = CommentForm () )
119
122
120
123
121
124
@main .route ('/delete_post/<int:post_id>' , methods = ['POST' ])
@@ -151,6 +154,7 @@ def edit_comment(comment_id):
151
154
form = EditCommentForm ()
152
155
if form .validate_on_submit ():
153
156
comment .content = form .content .data
157
+ comment .html_content = markdown .markdown (form .content .data ) # 解析 Markdown
154
158
session .commit ()
155
159
flash ('评论已更新!' , 'success' )
156
160
return redirect (url_for ('main.view_post' , post_id = comment .post_id ))
0 commit comments