Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8289533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:31:44+00:00 2026-06-08T12:31:44+00:00

I’m new to django I’m working on a voting application, and I need to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:31:46+00:00Added an answer on June 8, 2026 at 12:31 pm

    Solved

    so what I did is to add the vote processing inside the same view that display the books and use an if Post condition to detect when the vote button is clicked

    @render_to('user/list_books.html')  
    def list_books (request):    
        books_list = Book.objects.all()
    
      if request.POST:
        voted=request.session.get('has_voted',[])
        p_id=request.POST['id']
        book = Book.objects.get(id=p_id);
        if p_id in voted:         
            return {
                    'notvoted': book,
                    'error_message': "You have already voted for this book today!",
                    'books_list': books_list
                    }
        else:
            book.votes += 1
            book.save()
    
            vote = Vote() 
            vote.book_id = p_id
            vote.ip = request.META.get('REMOTE_ADDR')       
            vote.save()
    
            request.session['has_voted'] = voted+[p_id]
            request.session.set_expiry(86400)#one day in seconds
            return {
                     'books_list': books_list,
                     'voted' : 1 ,
                     'book' : book
                    }
    else :
        return {'books_list': books_list}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.