from django import template
register = template.Library()
# A|add:5 --> A에다가 5를 더한다는 의미
# A|sub:5 --> A가 value 인자가 되고, 뒤에 5가 arg 인자가 된다.
@register.filter # register로 인식
def sub(value, arg):
return value - arg
question_list.html
제목이 나오는 부분 위에 번호를 출력하는 부분 수정
{% if question_list %}
{% for question in question_list %}
<tr class="text-center">
{# 현재번호 = 전체건수 - 시작인덱스 - 현재 인덱스 +1#}
{#add 뒤에는 변수가 오지 못한다. + 더하기는 있는데 빼기는 없어서 직접 만들어야 한다. #}
<td>{{ question_list.paginator.count|sub:question_list.start_index|sub:forloop.counter0|add:1 }}</td>
<td>
<a href="{% url 'pybo:detail' question.id %}">{{ question.subject }}</a>
</td>
<td>{{ question.create_date }}</td>
</tr>
{% endfor %}
{% else %}
적용된 모습
질문에 달린 답변의 개수 표시하기
제목 뒤에 댓글이 달린 개수를 보여주자.
question_list.html 수정
{% if question_list %}
{% for question in question_list %}
<tr class="text-center">
{# 현재번호 = 전체건수 - 시작인덱스 - 현재 인덱스 +1#}
{#add 뒤에는 변수가 오지 못한다. + 더하기는 있는데 빼기는 없어서 직접 만들어야 한다. #}
<td>{{ question_list.paginator.count|sub:question_list.start_index|sub:forloop.counter0|add:1 }}</td>
<td>
<a href="{% url 'pybo:detail' question.id %}">{{ question.subject }}</a>
{% if question.answer_set.count > 0 %}
<span class = "text-danger small mx-2">{{ question.answer_set.count }}</span>
{% endif %}
</td>
<td>{{ question.create_date }}</td>
</tr>
{% endfor %}
{% else %}