jQuery、Ajax、および Django を使用してブログに投票システムを追加しようとしていますが、これを行うための最良の方法が見つかりません。
ここに私の質問があります:
- .post メソッドのパラメーターとして送信する必要がある jQuery スクリプトで{{ blog.id }}を取得するにはどうすればよいですか?
- jQuery スクリプトで、投票画像を変更するために使用する{% static %}パスを見つけるにはどうすればよいですか?
これが私がこれまでに得たものです...
votes.html
<a>Total: {{ total_votes }} </a>
<input type="hidden" name="blog_id" value={{ blog.id }}>
<div class="vote-buttons">
{% if vote.up %}
<img class="vote-up selected" src="{%static "assets/images/up_on.png"%}"/>
{% else %}
<img class="vote-up" src="{% static "assets/images/up_off.png" %}"/>
{% endif %}
{% if vote.down %}
<img class="vote-down selected" src="{%static "assets/images/down_on.png"%}"/>
{% else %}
<img class="vote-down" src="{% static "assets/images/down_off.png" %}"/>
{% endif %}
</div>
jQuery/Ajax
$(document).ready(function(){
$('.vote-up, .vote-down').css('cursor', 'pointer');
$('div.vote-buttons img.vote-up').click(function(){
if($(this).hasClass('selected')){
$.post('myurl', {params:params}, function(response){
$(this).removeAttr('src')
.attr('src',"...") # how to get the template {% static %} path?
.removeClass('selected');
});
}else{
# when vote isn't selected
}
});
});