장고/점프투장고

폼 + 상품의 총 구매수 {{ q.answer_set.count }} 출력

나도초딩 2023. 11. 12.

댓글 등록은 상품 예약하기와 같다.

2가지 방법

1) q.answer_set.create(subject=request.POST.get('subject') .............. )

Answer 객체를 사용하지 않기 때문에, save() 메소드를 사용하지 않는다.

def answer_create(request, question_id):
q = get_object_or_404(Question, pk=question_id)
q.answer_set.create(subject = request.POST.get('subject'), content = request.POST.get('content'), create_date=timezone.now())
return redirect('b2b:detail', q.id)

 

2) 일반적인 방법

q = get_object_or_404(Question, pk=question_id)
 a = Answer(q = q, subject = request.POST.get('subject'), ............. )

a.save()

 

2. 총 답변 수(상품 구매/예약 수)

{{ q.answer_set.count }}

 

{% for answer in q.answer_set.all %}

 

3. 상세페이지 - 댓글폼/예약폼/구매폼 구현

{% if q %}
<h1> {{ q.subject }}</h1>
<h3>{{q.create_date }}</h3>
<p>{{ q.content }}</p>
{% else %}
<h1>질문이 없떠요. 말이됨?</h1>
{% endif %}
<h5>총 예약인원 : {{ q.answer_set.count }}</h5>
<div>
    <ul>
        {% for answer in q.answer_set.all %}
        <h3>{{answer.subject}}</h3>
        <li>{{answer.content}} {{answer.create_date}}</li>
        {% endfor %}
    </ul>
</div>
<form action="{% url 'b2b:answer_create' q.id %}" method="post">
    {% csrf_token %}

    제목 : <input type="text" name="subject" id="subject"><br>
    내용 : <textarea name = "content" id="content" rows="15"></textarea><br>
    <input type="submit" value = "답변등록">
</form>

 

댓글