Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango: dynamically inserting comment form using jquery
    primarykey
    data
    text
    <p>I'm building a Q&amp;A site where one can comment on questions and their answers.Its a threaded commenting system with ajax.</p> <p><strong>this is the javascript part:</strong></p> <pre><code>function bindPostCommentHandler() { $('.commentFormWrapper form').submit(function() { var current = $(this); $.ajax({ type: "POST", data: current.serialize(), url: "{% comment_form_target %}", cache: false, dataType: "html", beforeSend:function(xhr){ $('.submit', current).html('&lt;img id="preloader" class="va-mid" src="{{MEDIA_URL}}img/indicator.gif" title="processing.." /&gt;'); $('#commentError').remove(); }, success: function(html, textStatus) { current.parent().replaceWith(html); bindPostCommentHandler(); }, error: function(xhr, textStatus, errorThrown) { $('#commentError').remove(); $('.submit', current).html('&lt;input type="submit" name="submit" class="submit-post small-button" value="Submit" /&gt;'); if(xhr.status == 400){ current.before('&lt;li id="commentError" class="no-bullet errornote margin10"&gt;OOPS ! your comment looked liked a spam. Try again with some modifications.&lt;/li&gt;'); }else { current.before('&lt;li id="commentError" class="no-bullet errornote margin10"&gt;Your comment was unable to be posted at this time. You may try after sometime.&lt;/li&gt;'); } //bindPostCommentHandler(); } }); return false; }); } $(document).ready(function() { bindPostCommentHandler(); }); </code></pre> <p><strong>the html part:</strong></p> <pre><code>&lt;!-- comment form for question --&gt; &lt;div class="commentFormWrapper"&gt; {% render_comment_form for question %} &lt;/div&gt; &lt;!-- comment form for answers --&gt; {% for answer in question.answers.all %} &lt;div class="commentFormWrapper"&gt; {% render_comment_form for answer %} &lt;/div&gt; </code></pre> <p><strong>The problem is</strong>, when there is only a single form in a page it works smoothly. With multiple forms its working but sending the request to the server muliple times(grows in multiples). </p> <p>Also, it would be better to dynamically insert/remove forms. But if I add the html for forms manually, I'll miss out csrf tokens and timestamp fields in the comment form. Anybody has solutions to these ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload