Skip to main content

Django vote base models and views

Project description


$ [sudo] pip install django-vote-base
from django.db import models
from django_vote_base.models import VoteModel

class CollectionVote(VoteModel):
    obj = models.ForeignKey(
        'Collection', related_name="vote_set", on_delete=models.CASCADE)

    class Meta:
        unique_together = [('obj', 'created_by')]

class Collection(models.Model):
    votes_up_count = models.IntegerField(null=True)
    votes_down_count = models.IntegerField(null=True)
    votes_score = models.IntegerField(null=True)

    def get_vote_down_url(self):
        return '/vote/collection/down/%s' % (,)

    def get_vote_up_url(self):
        return '/vote/collection/up/%s' % (,)
from django.urls import include, path

import views

urlpatterns = [
    path('vote/collection/down/<int:pk>', views.VoteDownView.as_view(), name='down'),
    path('vote/collection/up/<int:pk>', views.VoteUpView.as_view(), name='up')

DetailView - context variables {{ vote_obj }} and {{ vote }}

from apps.collections.models import Collection, CollectionVote
from django_vote_base.views import VoteViewMixin

class VoteDetailView(BookmarkViewMixin):
    vote_model = CollectionVote

XMLHttpRequest view

from django_vote_base.views import VoteDownView, VoteUpView
from apps.core.collections.models import Collection, CollectionVote

class CollectionVoteViewMixin:
    vote_model = CollectionVote

    def get_data(self):
        collection = Collection.objects.get(pk=self.kwargs['pk'])
        return {
            'votes_up_count': collection.votes_up_count,
            'votes_down_count': collection.votes_down_count,
            'votes_score': collection.votes_score

class VoteDownView(CollectionVoteViewMixin, VoteDownView):

class VoteUpView(CollectionVoteViewMixin, VoteUpView):

ListView prefetch user bookmarks

from django.db.models import Prefetch
from django.views.generic.list import ListView
from apps.collections.models import Collection, CollectionVote

class CollectionListView(ListView):
    def get_queryset(self, **kwargs):
        qs = Collection.objects.all()
        if self.request.user.is_authenticated:
            qs = qs.prefetch_related(
                Prefetch("vote_set", queryset=CollectionVote.objects.filter(created_by=self.request.user),to_attr='votes')
        return qs
<a data-id="{{ }}" class="vote-up-btn {% if vote and > 0 %}selected{% endif %}" {% if request.user.is_authenticated %}data-href="{{ vote_obj.get_vote_up_url }}"{% else %}href="{% url 'login' %}?next={{ request.path }}"{% endif %}>

<a data-id="{{ }}" class="votes-score">{{ vote_obj.votes_score|default:"0" }}</a>

<a data-id="{{ }}" class="vote-down-btn {% if vote and < 0 %}selected{% endif %}" {% if request.user.is_authenticated %}data-href="{{ vote_obj.get_vote_down_url }}"{% else %}href="{% url 'login' %}?next={{ request.path }}"{% endif %}>
const vote_buttons = document.querySelectorAll(",");
for (const button of vote_buttons) {
  button.addEventListener('click', function(event) {
    data_id = button.getAttribute('data-id');

    var vote_down_btn = document.querySelector(".vote-down-btn[data-id='"+data_id+"']");
    var vote_up_btn = document.querySelector(".vote-up-btn[data-id='"+data_id+"']");
    var votes_score = document.querySelector(".votes-score[data-id='"+data_id+"']");

    var xhr = new XMLHttpRequest();'GET', button.getAttribute('data-href'));
    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    xhr.responseType = 'json';
    xhr.onload = function() {
        if (xhr.status != 200) {
            console. error(`Error ${xhr.status}: ${xhr.statusText}`);
        if (xhr.status == 200) {

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-vote-base-2020.7.1.tar.gz (3.6 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page