简体   繁体   中英

Restrict each user to only vote once (Polls, django, python)

I found an similar question here , but unlike there and unlike in django official tutorial , I don't have a separate Choice class. How can I restrict every user to vote just one? What should I change in my code?

my models.py:

from django.contrib.auth.models import User
class Law(models.Model):
    #some code here
    yes_votes = models.IntegerField(default=0)
    no_votes = models.IntegerField(default=0)


class Voter(models.Model):
    user = models.ForeignKey(User)
    law = models.ForeignKey(Law)

my views.py:

class LawDetailView(generic.DetailView):
    model = Law
    template_name = 'law_detail.html'

    def get_queryset(self):
        """
        Excludes any petitions that aren't published yet.
        """
        return Law.objects.filter(pub_date__lte=timezone.now())


class LawResultsView(generic.DetailView):
    model = Law
    template_name = 'law_results.html'






def law_yes_vote(request, law_id):
    if Voter.objects.filter(law_id=law_id, user_id=request.user.id).exists():

        return render(request, 'law_detail.html', {
        'law': p,
        'error_message': "Sorry, but you have already voted."
        })

    else:
        p = get_object_or_404(Law, pk=law_id)
        p.yes_votes += 1
        p.save()

        return HttpResponseRedirect(reverse('laws:law_results', args=(p.id,)))



def law_no_vote(request, law_id):
    if Voter.objects.filter(law_id=law_id, user_id=request.user.id).exists():

        return render(request, 'law_detail.html', {
        'law': p,
        'error_message': "Sorry, but you have already voted."
        })

    else:
        p = get_object_or_404(Law, pk=law_id)
        p.no_votes += 1
        p.save()

        return HttpResponseRedirect(reverse('laws:law_results', args=(p.id,)))

my law_detail.html:

{% if request.user.is_authenticated %}
{% if error_message %}
<h1 >{{ error_message }}</h1>  

{% else %}

<div class="row" id="row-voting">
<form action="{% url 'laws:law_yes_vote' law.id %}" method="post">
{% csrf_token %}

<button class="btn btn-success" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" > 
<label >YES</label>
</form>
<form action="{% url 'laws:law_no_vote' law.id %}" method="post">
{% csrf_token %}

<button class="btn btn-danger" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" > 
<label >NO</label>
</form>
</div>
{% endif %}
{% else %}

<h1>Please, register</h1>

{% endif %}

It looks like you have forgotten to create the voter instance after the user has voted.

def law_yes_vote(request, law_id):
    if Voter.objects.filter(law_id=law_id, user_id=request.user.id).exists():

        return render(request, 'law_detail.html', {
            'law': p,
            'error_message': "Sorry, but you have already voted."
        })

    else:
        p = get_object_or_404(Law, pk=law_id)
        p.yes_votes += 1
        p.save()
        Voter.objects.create(law_id=law_id, user_id=request.user.id)

    return HttpResponseRedirect(reverse('laws:law_results', args=(p.id,)))

You'll need to update law_no_vote in the same way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM