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 7777951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:16:21+00:00 2026-06-01T18:16:21+00:00

On my index page I need to know if the current user has voted

  • 0

On my index page I need to know if the current user has voted on a book, so I can set a class on the <li> elements the current user has already voted on.

Template (snippet)

{% if top_books %}
<ul>
    {% for book in top_books %}
        <li id="{{ book.id }}" class="book">
                <img class="upvote {% ifequal thisUserUpVote 1 %}selected{% endifequal %}" title="Upvote book (click to undo)" src="{{ STATIC_URL }}images/upvote.png" />
                {{ book.score|floatformat }}
                <img class="downvote {% ifequal thisUserDownVote 1 %}selected{% endifequal %}" title="Downvote book (click to undo)" src="{{ STATIC_URL }}images/downvote.png" />
            <a href="/p/{{ book.id }}/">{{ book.title }}</a>
        </li>
    {% endfor %}
</ul>

Views.py (snippet)

from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from books.models import Book

def index(request):
    latest_book = Book.objects.all().order_by('-pub_date')[:5]
    top_items = Book.objects.all().order_by('-score')[:100]
    return render_to_response('books/index.html', {
                            'latest_books': latest_books,
                            'top_books' : top_books},
                            context_instance=RequestContext(request))

Models.py (snippet)

class Book(models.Model):
    title = models.CharField(max_length=500)
    pub_date = models.DateField()
    author = models.ManyToManyField(Author)
    userUpVotes = models.ManyToManyField(User, blank=True)
    userDownVotes = models.ManyToManyField(User, blank=True)
  • 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-01T18:16:23+00:00Added an answer on June 1, 2026 at 6:16 pm

    First, your code won’t render correctly since you are passing variables to the template that don’t exist in your view – I hope this is just a typo.

    def index(request):
        latest_book = Book.objects.all().order_by('-pub_date')[:5]
        top_items = Book.objects.all().order_by('-score')[:100]
        return render_to_response('books/index.html', {
                                'latest_books': latest_books, # latest_book
                                'top_books' : top_books}, # top_items
                                context_instance=RequestContext(request))
    

    For your question – you need to check if a user is userUpVotes or userDownVotes for the model, in your loop in the template:

    {% for book in top_books %}
       {% if user in book.userupvotes.all %}
           {{ user }} has upvoted
       {% endif %}
       {% if user in book.userdownvotes.all %}
           {{ user }} has downvoted
       {% endif %}
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to redirect user to another page called free.php from index page, if
Hai all i need to know how i can make next page in jQuery.
My index.html page for my project needs some Ruby code so I need to
I have an index page index.php , and when the user clicks on a
At current i have an index page which shows values from an XML page
I hava a url like mysite.net/home/index/page/XX while XX is any number. I need to
Situation: I have an index page which loads pages into it with ajax as
On my index page, I want the h1 text color to be white and
I have my index page which posts a single entry instead of the usual
I have an index page with three links as follws. When I click the

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.