I’m using the djangoratingsapp to create a system of “likes” and “dislike” buttons, and everything seems to work well. However, I am using a very basic button type of layout in the template and using the standard POST protocol to register button presses and enter ratings. My main problem with this approach is that there are a lot of comments on each page that a user can push like buttons for and the way I have this written reloads the page after each button press and brings the user back at the top of the page which is very annoying in practice for the end user. I am wondering what the easiest way to get the button presses registered without reloading the entire page in a Django App. I searched and am looking into comet. I am wondering if there is any easier way or if anyone wants to give me some hints, I’d much appreciate it. This is no fancy site, the only thing I need is button presses without page reloads, nothing else is necessary like live chatting or live status updates etc. Thanks in advance.
Here is the simple code I am using for a button in the template:
<form action="/myapp/r/{{pageid}}/" method="post">
{% csrf_token %}
<input type="submit" class="btn" value="+1" name="likebtn"/>
</form>
Here is the views.py code for the associated button:
if (request.POST.get('likebtn')):
votenum = instance.likerating.votes #get the number of total votes before casting a vote
pagelikerating = instance.likerating.add(score=2, user=request.user, ip_address=request.META['REMOTE_ADDR']) #use the djangoratings app to add the rating
if votenum != instance.likerating.votes: #check to see if the voting went through or got blocked from IP limit. If voting went through, the votenum variable will be 1 integer lower than the new queried total votes
instance.likeratingcounter += 1 #this is a simple integerfield in the model that serves as a counter for number of likes
instance.save() #save the addition of +1 on the counter
So I got the answer due to a bit of clarification from Daniel. Thanks Daniel! You can just use simple AJAX for them. Here is a nice guide for it too exactly for buttons on the tango with django tutorial: http://www.tangowithdjango.com/book/chapters/ajax.html