对于AJAX调用使用Dajaxice获取对象的值 [英] Getting object value for AJAX call using Dajaxice
问题描述
我有一个歌曲
模式与票
属性。我有一个投票收藏
每个歌曲
对象下面显示的按钮。我想,当用户点击投票收藏
按钮与相关联的票
属性宋
对象应加1和所有的投票收藏
按钮应该被禁用。
I have a Song
model with a votes
attribute. I have a Vote as Favourite
button displayed below each Song
object. I want when a user clicks on the Vote as Favourite
button the votes
attribute associated with that Song
object should increment by 1 and all the Vote as Favourite
buttons should be disabled.
HTML
{% for song in dj_song_list %}
<div>
<p class="song"><h3><strong>{{ song.name }}</strong></h3></p>
<p><strong>Artist: </strong>{{ song.artists}}</p>
<button type="button" class="btn btn-default btn-custom" id='vote' onclick="Dajaxice.hunt.disable_button(Dajax.process)">Vote as Favourite</button>
</div>
{% endfor %}
我使用dajaxice / dajax我的AJAX调用。这是我想出来的是我ajax.py
I'm using dajaxice/dajax for my AJAX call. This is what I've come up with as my ajax.py
ajax.py
from django.utils import simplejson
from dajaxice.decorators import dajaxice_register
from dajax.core import Dajax
@dajaxice_register
def disable_button(request):
dajax = Dajax()
dajax.assign('#vote', 'disabled', 'disabled')
return dajax.json()
@dajaxice_register(method="POST")
def update_votes(request, song):
song.votes += 1
song.save()
return simplejson.dumps({'message':'Thank you for voting'})
JS
function callback(data){
alert(data.message);
}
我如何获得歌曲
对象的值发送给 update_votes()
?
在 disable_button()
禁用只有一次投票按钮。如何禁用所有的投票按钮?
The disable_button()
disables only the first vote button. How can I disable all the vote buttons?
我怎么能说两种 update_votes()
和 disable_button()
使用相同的按钮onclick属性?
How can I call both update_votes()
and disable_button()
using the same buttons onclick attribute?
推荐答案
下面是你如何能更新的选票。你不需要update_votes是一个AJAX功能。
Here's how you can update the votes. You don't need update_votes to be an ajax function.
def update_votes(song_id):
song = Song.objects.get(id=song_id)
song.votes += 1
song.save()
您可以再从内部disable_button调用这个函数。
You can then call this function from within disable_button.
@dajaxice_register
def disable_button(request, song_id):
update_votes(song_id)
#disable the buttons
return #whatever
和你确定你使用的是正确的选择,以禁用按钮?
And are you sure you're using the right selector to disable your buttons?
这篇关于对于AJAX调用使用Dajaxice获取对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!