I'm new to django
I'm working on a voting application, and I need to stay on the same page after casting a vote
Meaning that I want to refresh the page without leaving it and display a success message
Here is my view.py (where I set and check cookies to avoid double votes):
@render_to('user/books_list.html')
def vote(request, object_id):
voted=request.session.get('has_voted',[])
books = Book.objects.get(id=object_id);
if object_id in voted:
return {
'object': books,
'error_message': "You have already voted.",
}
else:
books.votes += 1
books.save()
request.session['has_voted']=voted+[object_id]
return locals()
Here is my urls.py:
urlpatterns = patterns('user.views',
url(r'^books/$', 'books_list', name="books_list"),
url(r'^books/(?P<object_id>\d+)$', 'book_detail', name="book"),
url(r'^books/vote/(?P<object_id>\d+)$', 'vote', name="vote"),
)
Here is my template :
{% if list_participant %}
{% for book in list_books %}
{{ book.name }}
<a href={% url vote book.id %} >vote</a>
{{ book.votes }}
{% endfor %}
{% endif %}
The way I'm doing it now it redirect me to books/vote/x I'd like it to redirect to the previous page ,which is books/
Any idea please Thanks in advance