2015-10-07 68 views
1

我不是英文用戶,所以我的英文不好。如何在onclick事件中訪問模型的字段(django)

我在學習django。

我想要訪問模型的字段,當我點擊模板中的按鈕。

這是模板的相關部分。

<button type="button" onclick="{% post.like+=1 %}">1따봉!</button> 추천 수 : {{ post.like }} 

當我點擊按鈕我希望計數增加一。

我該怎麼做到這一點?

+0

你使用jQuery或JavaScript核心? – Prabhakaran8737

+0

我可以使用jQuery和Javascript,但現在我沒有使用它們。 – sojung

回答

0

您可以使用jQuery來做到這一點更容易

<button id="buttonLike" data-like="{{ post.like }}" type="button">{{ post.like }}</button> 
$(function() { 
     $('#buttonLike').on('click', function() { 
      var val = parseInt($(this).data('like')); 
      val += 1; 
      $(this).data('like', val); 
      $(this).val(val); 
      // Do some ajax operations to update the django model 
     }); 
    }); 
相關問題