2012-06-08 5 views
0

Nick Carroll의 방법 here을 사용하여 Django ajax 덧글을 내 앱에 구현했습니다. 나는 서버가 수신하고 저장 한 후에 ajax를 사용하여 페이지에 코멘트, 제출 날짜 및 코멘트를 게시 한 사용자를 표시하고 싶습니다. 이 작업을 수행하는 좋은 방법은 무엇입니까?ajax와 함께 제출 한 후 Django의 의견을 표시

답변

0

좋아이 이것에 대해 갈 수있는 hackerish 방법의 일종이다,하지만 난 괜찮은 솔루션을 알아 냈어요 생각 :이 넣어 그래서 자바 스크립트에 비교적 새로운 해요

<script type="text/javascript" charset="utf-8"> 
    function bindPostCommentHandler() { 
     $('#comment_form form input.submit-preview').remove(); 
     $('#comment_form form').submit(function() { 
      var comment_text = $('#id_comment').val(); 
      $.ajax({ 
       type: "POST", 
       data: $('#comment_form form').serialize(), 
       url: "{% comment_form_target %}", 
       cache: false, 
       dataType: "html", 
       success: function(html, textStatus) { 
        $('#comment_form form').fadeTo(500, 0, function(){ 
         $(this).remove(); 
        }); 

        var today = new Date(); 
        var dd = today.getDate(); 
        var mm = today.getMonth()+1; 
        var yyyy = today.getFullYear(); 
        if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} var today = mm+'/'+dd+'/'+yyyy; 
        var comment = "<div class='comment'><h4>User " + "\"{{ user.username }}\"" + " Rating <small>" + today + "</small></h4>" + comment_text + "</div><hr />"; 
        $(comment).hide().prependTo("#comments_loc").fadeIn(1000); 
        bindPostCommentHandler(); 
       }, 
       error: function (XMLHttpRequest, textStatus, errorThrown) { 
        $('#comment_form form').replaceWith('Your comment was unable to be posted at this time. We apologize for the inconvenience.'); 
       } 
      }); 
     return false; 
    }); 
} 

$(document).ready(function() { 
    bindPostCommentHandler(); 
}); 
</script> 

을 내가 아는 것과 함께. 이 문제를 해결할 수 있다고 생각되면 의견을 남기십시오.

0

jquery의 post 메소드를 사용하여 서버에 주석을 게시하고 응답이 성공하면 success call back 함수에서 페이지에 주석을 표시하십시오.

-1
<script type="text/javascript" charset="utf-8"> 
    function bindPostCommentHandler() { 
     $('#comment_form form input.submit-preview').remove(); 
     $('#comment_form form').submit(function() { 
      $.ajax({ 
       type: "POST", 
       data: $('#comment_form form').serialize(), 
       url: "{% comment_form_target %}", 
       cache: false, 
       dataType: "html", 
       success: function(html, textStatus) { 
        $('#comment_form form').replaceWith(html); 
        $('.comt_message').show(); 
        bindPostCommentHandler(); 
       }, 
       error: function (XMLHttpRequest, textStatus, errorThrown) { 
        $('#comment_form form').replaceWith('Your comment was unable to be posted at this time. We apologise for the inconvenience.'); 
       } 
      }); 
      return false; 
     }); 
    } 

    $(document).ready(function() { 
     bindPostCommentHandler(); 
    }); 
    </script> 
+0

그 점은 많이 이해하지만 해당 div에로드 할 댓글 정보를 어떻게 얻을 수 있습니까? 나는 여기서 큰 것을 놓치고있을거야. – HighLife

관련 문제