【问题标题】:How to make it such that if comments have been edited before, an "edited" permanent message will be displayed beside the comment?如果之前已经编辑过评论,如何使其在评论旁边显示“已编辑”的永久消息?
【发布时间】:2021-04-29 04:51:20
【问题描述】:

如果之前已编辑过 cmets,如何使其在评论旁边显示“已编辑”永久消息?这样大家就可以看到评论之前已经被编辑过了。 (如果我也可以保留原始预编辑消息的副本会很好,但如果那太难了,我只是希望在评论旁边显示一条“已编辑”的永久消息。

models.py

class Comment(models.Model):
   post = models.ForeignKey(BlogPost, related_name='comments', on_delete=models.CASCADE)
   name = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='name', on_delete=models.CASCADE)
   body = models.TextField()

class BlogPost(models.Model):
 title                  = models.CharField(max_length=50, null=False, blank=False, unique=True)
 author                     = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
 slug                   = models.SlugField(blank=True, unique=True)

views.py

def edit_own_comment(request, post_id):
    context = {}
    comment = get_object_or_404(Comment, id=post_id)
    if request.method == 'POST':
        form = UpdateCommentForm(request.POST or None, instance=comment)
        if form.is_valid():
            obj.save()
            messages.success(request, 'Your comment has been edited', extra_tags='editedcomment')
            return redirect(reverse("HomeFeed:detail", kwargs={'slug': comment.post.slug }))

    form = UpdateCommentForm(
            initial = {
                    "body": comment.body,
            }
        )

    context['form'] = form
    return render(request, 'HomeFeed/edit_comment.html', context)


class DetailBlogPostView(BlogPostMixin,DetailView):
    template_name = 'HomeFeed/detail_blog.html'
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        blog_post=self.get_object()
        blog_post.save()

forms.py

class UpdateCommentForm(forms.ModelForm):
 class Meta:
  model = Comment
  fields = ['body']

 def save(self, commit=True):
  comment = self.instance
  comment.body = self.cleaned_data['body']

  if commit:
   comment.save()
  return comment

detail.html

  {% for comment in blog_post.comments.all %}
    {{ comment.name}}
  {{ comment.body }}
  {% endfor %}

【问题讨论】:

    标签: django django-models django-views django-forms django-templates


    【解决方案1】:

    您可以在数据库中使用“已编辑”布尔字段,该字段将用作标志(默认情况下为 False 值)。每当更新操作发生时,您都会将此标志设置为 True。您可以在模板中使用此字段,使“已编辑”文本在消息之前可见。

    【讨论】:

    • 嗨@Ankush,感谢您的输入,所以基本上我需要一个新字段?如果没有新领域,我就没有办法做到这一点吗?如果是这样,您能否分享我如何将该字段“连接”到更新操作?这意味着当我更新时如何将此标志设置为 True?
    【解决方案2】:

    好吧,Ankush 描述了您可以拥有的最佳解决方案,请将其添加到您的 Commentmodel

    edited = models.BooleanField(default=False)
    

    然后在您的视图中,只要在调用comment.save() 之前编辑了评论,请执行以下操作:

    comment.edited = True
    

    在您列出 cmets 的 html 中执行以下操作:

    {% for comment in comments %}
        <p>{{ comment.name }} : {{ comment.body }}</p>
        {% if comment.edited %}
            <p>edited</p>
        {% endif %}
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2015-02-10
      • 1970-01-01
      • 2015-08-16
      • 2022-07-13
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2020-05-27
      • 2016-05-21
      相关资源
      最近更新 更多