Compare commits
2 Commits
master
...
ytc_branch
| Author | SHA1 | Date |
|---|---|---|
|
|
90b7783a2b | 3 months ago |
|
|
5d48b79aca | 4 months ago |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,10 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CommentsConfig(AppConfig):
|
||||
# 指定该 App 在项目中的名称(即所在目录名)
|
||||
name = 'comments'
|
||||
|
||||
# (可选)可以在这里做初始化操作,如引入 signals
|
||||
# def ready(self):
|
||||
# import comments.signals
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,42 @@
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views.generic import View
|
||||
|
||||
from blog.models import Article
|
||||
from .models import Comment
|
||||
from .forms import CommentForm
|
||||
|
||||
|
||||
class CommentPostView(LoginRequiredMixin, View):
|
||||
"""
|
||||
负责处理评论提交
|
||||
"""
|
||||
|
||||
def post(self, request, article_id):
|
||||
# 获取目标文章
|
||||
article = get_object_or_404(Article, pk=article_id)
|
||||
|
||||
form = CommentForm(request.POST)
|
||||
if form.is_valid():
|
||||
# 获取评论内容
|
||||
body = form.cleaned_data['body'].strip()
|
||||
parent_id = form.cleaned_data.get('parent_comment_id')
|
||||
|
||||
comment = Comment()
|
||||
comment.article = article
|
||||
comment.author = request.user
|
||||
comment.body = body
|
||||
|
||||
# 判断是否是子评论(回复)
|
||||
if parent_id:
|
||||
try:
|
||||
parent_comment = Comment.objects.get(id=parent_id)
|
||||
comment.parent_comment = parent_comment
|
||||
except Comment.DoesNotExist:
|
||||
pass
|
||||
|
||||
comment.save()
|
||||
|
||||
# 评论成功后返回文章页面
|
||||
return HttpResponseRedirect(article.get_absolute_url())
|
||||
Loading…
Reference in new issue