Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. '''
  2. To jest fragment kodu z pliku views.py
  3. '''
  4. def vote(request, question_id):
  5.     question = get_object_or_404(Question, pk=question_id)
  6.     try:
  7.         selected_choice = question.choice_set.get(pk=request.POST['choice'])
  8.     except (KeyError, Choice.DoesNotExist):
  9.         return render(request, 'polls/detail.html', {
  10.             'question': question,
  11.             'error_message': "You didn't select a choice.",
  12.         })
  13.     else:
  14.         selected_choice.votes += 1
  15.         selected_choice.save()
  16.         return HttpResponseRedirect(reverse('polls:results', args=(question_id,)))
  17.  
  18. '''
  19. To jest fragment kodu z models.py
  20. '''
  21. class Choice(models.Model):
  22.     question = models.ForeignKey(Question, on_delete = models.CASCADE)
  23.     choice_text = models.CharField(max_length = 200)
  24.     votes = models.IntegerField(default = 0)
  25.  
  26.     def __str__(self):
  27.         return self.choice_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement