master
pk6lqz7py 4 weeks ago
parent 83a8a95d2c
commit 23aa1e88a6

@ -11,21 +11,24 @@ from blog.models import Article
from .forms import CommentForm from .forms import CommentForm
from .models import Comment from .models import Comment
# 评论提交处理视图
class CommentPostView(FormView): class CommentPostView(FormView):
form_class = CommentForm form_class = CommentForm# 指定使用的表单类
template_name = 'blog/article_detail.html' template_name = 'blog/article_detail.html'# 表单无效时渲染的模板
#为视图添加CSRF保护
@method_decorator(csrf_protect) @method_decorator(csrf_protect)
def dispatch(self, *args, **kwargs): def dispatch(self, *args, **kwargs):
return super(CommentPostView, self).dispatch(*args, **kwargs) return super(CommentPostView, self).dispatch(*args, **kwargs)
# 处理GET请求直接访问评论提交URL时重定向到文章详情页评论区
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
article_id = self.kwargs['article_id'] article_id = self.kwargs['article_id']
article = get_object_or_404(Article, pk=article_id) article = get_object_or_404(Article, pk=article_id)
url = article.get_absolute_url() url = article.get_absolute_url()
return HttpResponseRedirect(url + "#comments") return HttpResponseRedirect(url + "#comments")
# 表单验证失败时的处理:重新渲染文章详情页并显示表单错误
def form_invalid(self, form): def form_invalid(self, form):
article_id = self.kwargs['article_id'] article_id = self.kwargs['article_id']
article = get_object_or_404(Article, pk=article_id) article = get_object_or_404(Article, pk=article_id)
@ -35,29 +38,35 @@ class CommentPostView(FormView):
'article': article 'article': article
}) })
# 表单验证成功后的处理:保存评论并发送通知邮件
def form_valid(self, form): def form_valid(self, form):
"""提交的数据验证合法后的逻辑""" """提交的数据验证合法后的逻辑"""
user = self.request.user user = self.request.user# 获取当前登录用户
author = BlogUser.objects.get(pk=user.pk) author = BlogUser.objects.get(pk=user.pk)# 获取完整的用户信息
article_id = self.kwargs['article_id'] article_id = self.kwargs['article_id']
article = get_object_or_404(Article, pk=article_id) article = get_object_or_404(Article, pk=article_id)
# 检查文章是否允许评论(评论状态或文章状态为关闭时抛出异常)
if article.comment_status == 'c' or article.status == 'c': if article.comment_status == 'c' or article.status == 'c':
raise ValidationError("该文章评论已关闭.") raise ValidationError("该文章评论已关闭.")
# 创建评论实例但不保存到数据库
comment = form.save(False) comment = form.save(False)
comment.article = article comment.article = article
# 获取站点配置,判断评论是否需要审核
from djangoblog.utils import get_blog_setting from djangoblog.utils import get_blog_setting
settings = get_blog_setting() settings = get_blog_setting()
if not settings.comment_need_review: if not settings.comment_need_review:
comment.is_enable = True comment.is_enable = True# 无需审核则直接启用评论
comment.author = author comment.author = author# 设置评论作者
# 处理父评论ID如果存在则设置为回复评论
if form.cleaned_data['parent_comment_id']: if form.cleaned_data['parent_comment_id']:
parent_comment = Comment.objects.get( parent_comment = Comment.objects.get(
pk=form.cleaned_data['parent_comment_id']) pk=form.cleaned_data['parent_comment_id'])
comment.parent_comment = parent_comment comment.parent_comment = parent_comment
# 保存评论到数据库
comment.save(True) comment.save(True)
# 重定向到文章详情页并定位到该评论锚点位置
return HttpResponseRedirect( return HttpResponseRedirect(
"%s#div-comment-%d" % "%s#div-comment-%d" %
(article.get_absolute_url(), comment.pk)) (article.get_absolute_url(), comment.pk))

Loading…
Cancel
Save