Уведомления

Группа в Telegram: @pythonsu

#1 Март 19, 2019 20:07:44

dtnis19
Зарегистрирован: 2019-03-04
Сообщения: 3
Репутация: +  0  -
Профиль   Отправить e-mail  

Передача контекста в Django

Почему шаблонизатор не видит переменную count_votes.
Шаблон:

 {% extends "polls/wrapper.html" %}
{% block content %}
<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>
<p>Всего голосов: {{count_votes}}</p>
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
{% endblock %}
———————-
Представление:
 def results(request, question_id):
    question = get_object_or_404(Question,pk=question_id)
    count_votes
    for choice in choice_set.all():
        count_votes += choice.votes
    return render(request,'polls//results.html',{'question': question,'count_votes': str(count_votes),})

Отредактировано dtnis19 (Март 20, 2019 11:56:39)

Офлайн

#2 Март 19, 2019 21:49:18

Romissevd
От: Счастье
Зарегистрирован: 2015-03-01
Сообщения: 533
Репутация: +  76  -
Профиль   Отправить e-mail  

Передача контекста в Django

А так?

 def results(request, question_id):
    question = get_object_or_404(Question,pk=question_id)
    count_votes = 0
    for choice in choice_set.all():
        count_votes += choice.votes
    return render(request,'polls//results.html',{'question': question,'count_votes': str(count_votes),})

Отредактировано Romissevd (Март 19, 2019 21:54:38)

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version