파이썬

vote() 뷰함수와 템플릿

mcdn 2020. 9. 20. 16:49
반응형
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,)))

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/results.html', {'question':question})
<h1>{{ question.question_text }}</h1>
<ul>
    {% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }} - {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
    {% endfor %}
</ul>

<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
반응형

'파이썬' 카테고리의 다른 글

파이썬 유튜브 추천 링크들  (0) 2020.10.05
admin사이트 제목 고치기  (0) 2020.09.22
admin.py 고치기  (0) 2020.09.20
ch3 결과 polls  (0) 2020.09.20
detail()뷰함수와 템플릿 파일  (0) 2020.09.20
urlconf 코딩해보기  (0) 2020.09.20
admin에 테이블 등록해보기  (0) 2020.09.19
MVT - Models 내용  (0) 2020.09.19