|
|
|
|
@ -13,51 +13,66 @@ from .models import Comment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommentPostView(FormView):
|
|
|
|
|
form_class = CommentForm
|
|
|
|
|
template_name = 'blog/article_detail.html'
|
|
|
|
|
"""处理评论提交的视图类,继承自Django的FormView"""
|
|
|
|
|
|
|
|
|
|
@method_decorator(csrf_protect)
|
|
|
|
|
form_class = CommentForm # 指定使用的表单类
|
|
|
|
|
template_name = 'blog/article_detail.html' # 指定渲染的模板
|
|
|
|
|
|
|
|
|
|
@method_decorator(csrf_protect) # 添加CSRF保护装饰器,防止跨站请求伪造
|
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
|
"""重写dispatch方法,在请求处理前后执行额外逻辑"""
|
|
|
|
|
return super(CommentPostView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
article_id = self.kwargs['article_id']
|
|
|
|
|
article = get_object_or_404(Article, pk=article_id)
|
|
|
|
|
url = article.get_absolute_url()
|
|
|
|
|
return HttpResponseRedirect(url + "#comments")
|
|
|
|
|
"""处理GET请求 - 重定向到文章详情页的评论区域"""
|
|
|
|
|
article_id = self.kwargs['article_id'] # 从URL参数获取文章ID
|
|
|
|
|
article = get_object_or_404(Article, pk=article_id) # 获取文章对象,不存在则返回404
|
|
|
|
|
url = article.get_absolute_url() # 获取文章的绝对URL
|
|
|
|
|
return HttpResponseRedirect(url + "#comments") # 重定向到文章页的评论锚点
|
|
|
|
|
|
|
|
|
|
def form_invalid(self, form):
|
|
|
|
|
article_id = self.kwargs['article_id']
|
|
|
|
|
article = get_object_or_404(Article, pk=article_id)
|
|
|
|
|
"""表单验证失败时的处理逻辑"""
|
|
|
|
|
article_id = self.kwargs['article_id'] # 获取文章ID
|
|
|
|
|
article = get_object_or_404(Article, pk=article_id) # 获取文章对象
|
|
|
|
|
|
|
|
|
|
# 重新渲染页面,显示表单错误信息
|
|
|
|
|
return self.render_to_response({
|
|
|
|
|
'form': form,
|
|
|
|
|
'article': article
|
|
|
|
|
'form': form, # 包含错误信息的表单
|
|
|
|
|
'article': article # 文章对象
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
"""提交的数据验证合法后的逻辑"""
|
|
|
|
|
user = self.request.user
|
|
|
|
|
author = BlogUser.objects.get(pk=user.pk)
|
|
|
|
|
article_id = self.kwargs['article_id']
|
|
|
|
|
article = get_object_or_404(Article, pk=article_id)
|
|
|
|
|
user = self.request.user # 获取当前登录用户
|
|
|
|
|
author = BlogUser.objects.get(pk=user.pk) # 获取对应的博客用户对象
|
|
|
|
|
article_id = self.kwargs['article_id'] # 从URL参数获取文章ID
|
|
|
|
|
article = get_object_or_404(Article, pk=article_id) # 获取文章对象
|
|
|
|
|
|
|
|
|
|
# 检查文章是否允许评论
|
|
|
|
|
if article.comment_status == 'c' or article.status == 'c':
|
|
|
|
|
raise ValidationError("该文章评论已关闭.")
|
|
|
|
|
comment = form.save(False)
|
|
|
|
|
comment.article = article
|
|
|
|
|
raise ValidationError("该文章评论已关闭.") # 抛出验证错误
|
|
|
|
|
|
|
|
|
|
comment = form.save(False) # 创建评论对象但不保存到数据库
|
|
|
|
|
comment.article = article # 设置评论关联的文章
|
|
|
|
|
|
|
|
|
|
from djangoblog.utils import get_blog_setting
|
|
|
|
|
settings = get_blog_setting()
|
|
|
|
|
settings = get_blog_setting() # 获取博客设置
|
|
|
|
|
|
|
|
|
|
# 如果博客设置不需要审核评论,则直接启用评论
|
|
|
|
|
if not settings.comment_need_review:
|
|
|
|
|
comment.is_enable = True
|
|
|
|
|
comment.author = author
|
|
|
|
|
|
|
|
|
|
comment.author = author # 设置评论作者
|
|
|
|
|
|
|
|
|
|
# 处理父级评论(回复功能)
|
|
|
|
|
if form.cleaned_data['parent_comment_id']:
|
|
|
|
|
parent_comment = Comment.objects.get(
|
|
|
|
|
pk=form.cleaned_data['parent_comment_id'])
|
|
|
|
|
comment.parent_comment = parent_comment
|
|
|
|
|
pk=form.cleaned_data['parent_comment_id']) # 获取父级评论对象
|
|
|
|
|
comment.parent_comment = parent_comment # 设置评论的父级评论
|
|
|
|
|
|
|
|
|
|
comment.save(True) # 保存评论到数据库
|
|
|
|
|
|
|
|
|
|
comment.save(True)
|
|
|
|
|
# 重定向到文章页并跳转到新评论的位置
|
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
|
"%s#div-comment-%d" %
|
|
|
|
|
(article.get_absolute_url(), comment.pk))
|
|
|
|
|
"%s#div-comment-%d" % # 使用锚点跳转到特定评论
|
|
|
|
|
(article.get_absolute_url(), comment.pk))
|