Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

django - How to make it such that if comments have been edited before, an "edited" permanent message will be displayed beside the comment?

How to make it such that if comments have been edited before, an "edited" permanent message will be displayed beside the comment? So that everyone will be able to see that the comment has been edited before. (it'll be good if i can keep a copy of the original pre-edited message too, but if thats too difficult, i'm just hoping to display an "edited" permanent message will be displayed beside the comment.

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 %}

question from:https://stackoverflow.com/questions/65883818/how-to-make-it-such-that-if-comments-have-been-edited-before-an-edited-perman

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well Ankush described the best possible solution you can have, add this to your Comment model:

edited = models.BooleanField(default=False)

Then in your view whenever the comment gets edited before calling comment.save() do this:

comment.edited = True

And in your html where you list comments do this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...