Django receiving duplicate signals when updating content from HTML page

I am getting duplicate of each content when updating content from html page. see the picture: enter image description here

I am not getting any duplicate when updating from Django admin panel. How to avoid duplicate when updating from HTML template?

here is my code

I think I am doing any mistake in my views. here is my

class ApproveCommentPage(UpdateView):
      model = BlogComment
      template_name = "blog/approve_comment.html"
      form_class =AdminComment
      def form_valid(self, form):
        self.object =
        status = self.object.is_published
        name =[0:20]
        messages.success(self.request, f"{name} comment status {status}")
        return super().form_valid(form)
      def get_success_url(self):
        res = reverse('blog:blog-admin')
        if 'page' in self.request.GET:
            res += f"?page={self.request.GET['page']}"
        return res

#Updating question

here is my notifications.html where rendering all messages:


{% for notification in  notifications  %} 
{%if == %}
{% if notification.notification_type == "Comment Approved" %}

 {{ notification.text_preview }}

def ShowNOtifications(request):
    notifications = Notifications.objects.all().order_by('-date')
    template_name ='blog/notifications.html'

    context = {
        'notifications': notifications,
    return render(request,template_name,context)

my html page where I am updating comment status. By default all comment status “pending” which means unpublished. here I am reviewing every comment and changing their status to “published”. Only getting duplicate when changing status from here. approve_commnet.html

<form  method="POST">
            {% csrf_token %}
            <button class="btn btn-info">Publish</button>

When updating comment status from admin panel I am not getting any duplicate. Only getting duplicate When trying to update comment status from approve_commnet.html .

In your form_valid method, the first line of that method saves the form.

The docs for UpdateView show that it inherits from ModelFormMixin.

ModelFormMixin is what defines the parent method form_valid.

From the docs for form_valid:

Saves the form instance, sets the current object for the view, and redirects to get_success_url() .
(Emphasis mine)

So by calling super in that method, you’re calling the parent class’ form_valid method, which is also saving the form. Each time the form is saved, the underlying model is saved, therefore generating two signals.

1 Like

KenWhitesell Thanks for your comment. see my updated question . I put get_success_url() but still now getting duplicate

Why do you think that replacing get_success_url is going to resolve this, when the issue is with form_valid?

KenWhitesell I was doing mistake here 'self.object = ’ then it’s again saving from return super().form_valid(form) I didn’t knew return super().form_valid(form) using for saving froms. Actually I was saving froms two time. am I right?

1 Like

That’s correct - that’s the issue.

1 Like