2016-07-12 3 views
0

Я нашел аналогичный вопрос here, но в отличие от него и в отличие от официального учебника django у меня нет отдельного класса Choice. Как я могу запретить каждому пользователю голосовать только один? Что я должен изменить в своем коде?Ограничить каждого пользователя, чтобы голосовать только один раз (Опросы, django, python)

мой 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) 

мой 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,))) 

мой 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 %} 
+0

В зависимости от того, насколько это важно. Если это пример игрушек, добавление логического 'has_voted' в класс' Voter' и установка его в вашем представлении после записи голоса, вероятно, будет достаточным. Если это «по-настоящему», у вас могут быть все виды поддельных атак, и этот вопрос, вероятно, слишком широк. –

+0

хорошая идея, спасибо! – Vasile

ответ

1

Похоже, что вы забыли создать voter после того, как пользователь проголосовал.

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,))) 

Аналогичным образом вам необходимо обновить law_no_vote.

Смежные вопросы