यहाँ मेरी समाधान का सार है। मैं क्लिक को संभालने के लिए jQuery/AJAX वाली छवियों का उपयोग करता हूं। इस साइट से काफी प्रभावित है। कुछ चीजें हैं जो कुछ काम (क्लाइंट में त्रुटि प्रबंधन, उदाहरण के लिए - और इसमें से अधिकतर रिफैक्टर किया जा सकता है) का उपयोग कर सकते हैं लेकिन उम्मीद है कि कोड आपके लिए उपयोगी है।
HTML:
<div class="vote-buttons">
{% ifequal thisUserUpVote 0 %}
<img class="vote-up" src = "images/vote-up-off.png" title="Vote this thread UP. (click again to undo)" />
{% else %}
<img class="vote-up selected" src = "images/vote-up-on.png" title="Vote this thread UP. (click again to undo)" />
{% endifequal %}
{% ifequal thisUserDownVote 0 %}
<img class="vote-down" src = "images/vote-down-off.png" title="Vote this thread DOWN if it is innapropriate or incorrect. (click again to undo)" />
{% else %}
<img class="vote-down selected" src = "images/vote-down-on.png" title="Vote this thread DOWN if it is innapropriate or incorrect. (click again to undo)" />
{% endifequal %}
</div> <!-- .votebuttons -->
jQuery:
$(document).ready(function() {
$('div.vote-buttons img.vote-up').click(function() {
var id = {{ thread.id }};
var vote_type = 'up';
if ($(this).hasClass('selected')) {
var vote_action = 'recall-vote'
$.post('/ajax/thread/vote', {id:id, type:vote_type, action:vote_action}, function(response) {
if (isInt(response)) {
$('img.vote-up').removeAttr('src')
.attr('src', 'images/vote-up-off.png')
.removeClass('selected');
$('div.vote-tally span.num').html(response);
}
});
} else {
var vote_action = 'vote'
$.post('/ajax/thread/vote', {id:id, type:vote_type, action:vote_action}, function(response) {
if (isInt(response)) {
$('img.vote-up').removeAttr('src')
.attr('src', 'images/vote-up-on.png')
.addClass('selected');
$('div.vote-tally span.num').html(response);
}
});
}
});
Django का मानना है कि AJAX अनुरोध संभालता है:
def vote(request):
thread_id = int(request.POST.get('id'))
vote_type = request.POST.get('type')
vote_action = request.POST.get('action')
thread = get_object_or_404(Thread, pk=thread_id)
thisUserUpVote = thread.userUpVotes.filter(id = request.user.id).count()
thisUserDownVote = thread.userDownVotes.filter(id = request.user.id).count()
if (vote_action == 'vote'):
if (thisUserUpVote == 0) and (thisUserDownVote == 0):
if (vote_type == 'up'):
thread.userUpVotes.add(request.user)
elif (vote_type == 'down'):
thread.userDownVotes.add(request.user)
else:
return HttpResponse('error-unknown vote type')
else:
return HttpResponse('error - already voted', thisUserUpVote, thisUserDownVote)
elif (vote_action == 'recall-vote'):
if (vote_type == 'up') and (thisUserUpVote == 1):
thread.userUpVotes.remove(request.user)
elif (vote_type == 'down') and (thisUserDownVote ==1):
thread.userDownVotes.remove(request.user)
else:
return HttpResponse('error - unknown vote type or no vote to recall')
else:
return HttpResponse('error - bad action')
num_votes = thread.userUpVotes.count() - thread.userDownVotes.count()
return HttpResponse(num_votes)
और धागा मॉडल के प्रासंगिक भागों:
class Thread(models.Model):
# ...
userUpVotes = models.ManyToManyField(User, blank=True, related_name='threadUpVotes')
userDownVotes = models.ManyToManyField(User, blank=True, related_name='threadDownVotes')
स्रोत
2009-10-07 04:55:55
यह उपयोगी हो सकता है। बस इसे अभी देख रहे हैं। क्या आप जानते हैं कि devdocs.apps.kb.models से आयात करें लिंक को बदला जाना चाहिए? –
मेरे उत्तर में कोड के इस पर लाभ प्रगतिशील वृद्धि है - यह जावास्क्रिप्ट के बिना काम करेगा, लेकिन आप एक बेहतर उपयोगकर्ता अनुभव बनाने के लिए शीर्ष पर AJAX जोड़ सकते हैं। –
devdocs.apps.kb.models को अपने models.py फ़ाइल के पथ के साथ बदलें जहां आप लिंक को परिभाषित करते हैं। यह आपकेprojectname.yourappname.models की तरह कुछ होगा। –