【发布时间】:2013-09-28 11:55:21
【问题描述】:
我有点卡住了,我正在尝试使用 jQuery、Ajax 和 Django 在我的博客中添加一个投票系统,但我没有找到最好的方法。
这是我的问题:
- 如何在我的 jQuery 脚本中获取 {{ blog.id }},该脚本必须作为 .post 方法中的参数发送?
- 在我的 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
}
});
});
【问题讨论】: