Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- To jest fragment kodu z pliku views.py
- '''
- def vote(request, question_id):
- question = get_object_or_404(Question, pk=question_id)
- try:
- selected_choice = question.choice_set.get(pk=request.POST['choice'])
- except (KeyError, Choice.DoesNotExist):
- return render(request, 'polls/detail.html', {
- 'question': question,
- 'error_message': "You didn't select a choice.",
- })
- else:
- selected_choice.votes += 1
- selected_choice.save()
- return HttpResponseRedirect(reverse('polls:results', args=(question_id,)))
- '''
- To jest fragment kodu z models.py
- '''
- class Choice(models.Model):
- question = models.ForeignKey(Question, on_delete = models.CASCADE)
- choice_text = models.CharField(max_length = 200)
- votes = models.IntegerField(default = 0)
- def __str__(self):
- return self.choice_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement