Question

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><h3>{{ song.name }}</h3></p>
        <p class="song_id">{{ song.song_id }}</p>
        <button type="button" class="btn btn-default btn-custom" class='vote' onclick="update();">Vote as Favourite</button>
    </div>
{% endfor %}

ajax.py

def update_votes(id):
    song = Song.objects.get(song_id=id)
    song.votes += 1
    song.save()

@dajaxice_register
def update_disable(request, song_id):
    update_votes(song_id)
    dajax = Dajax()
    dajax.assign('.vote', 'disabled', 'disabled')
    return dajax.json()

JavaScript

function update(){
     Dajaxice.hunt.update_disable(Dajax.process,{'song_id':$('.song_id').val()})
}

The button disabling part works fine when used alone. But when I use the update_votes() function in the update_disable() function nothing works. What is wrong with my code?

Was it helpful?

Solution 2

I had set the votes attribute of the Song model to default=None. Hence it wasn't able to perform the addition operation. I set it now as default=0 instead.

OTHER TIPS

AJAX part does nothing, because it gets errors. The problem is in the update_votes() function. The problems could be:

  • Several objects exist with same song_id value
  • The song with id does not exist

Try using pdb.set_trace() for debugging purposes:

def update_votes(id):
    import pdb; pdb.set_trace()
    song = Song.objects.get(song_id=id)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top