0

I have a django template like this :

{% for comment in service.comments.all %}
{{comment.comment}} -- commented by {{comment.user}}<br/>
{% for reply in comment.comment_replies.all %}

{{reply.comment_reply}} -- replied by {{reply.user}}<br/>

{% endfor %}
<form id="comment-replyform" action="/comment/reply/{{comment.id}}/" method="post">{% csrf_token %}

  {{ comment_reply_form.as_p }}

<input type="submit" name="submit" value="Reply">
</form>

{% empty %}
No comments 
{% endfor %}

Here I want to reply to comment using ajax.. so that page wont refresh.. I have written javascript code for this ..

$('#comment-replyform').on('submit', function(event){
    event.preventDefault();
  $.ajax({
           url: $(this).attr('href'),
           success: function(data) {
                    alert("Success");

            }
            });

    })

Here I see a success alert for only first comment reply form.. When I send from another comment reply field( because there might be many comment reply form for many comments).. It does not call ajax and refresh the page...

How can I post this form with comment message ?

1 Answer 1

2

HTML ids must be unique. You can't have multiple forms all with the same id. Use a class instead.

<form class="comment-replyform" action="/comment/reply/{{comment.id}}/" method="post">

$('.comment-replyform').on(...)

Note that you don't seem to be doing anything with the form contents though, so the submission still won't actually work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.