댓글 등록은 상품 예약하기와 같다.
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)
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>
'장고 > 점프투장고' 카테고리의 다른 글
폼 에러 표시. class , form.errors/field.label/field.errors (0) | 2023.11.13 |
---|---|
forms.py 에서 폼디자인( form.as_p 폼위젯, 수동폼 사용) (0) | 2023.11.12 |
테이블 모양 / 부트스트랩 (0) | 2023.11.12 |
settings.py : templates , static, media_url (0) | 2023.11.12 |
NoReverseMatch at /b2b/2/ Reverse for '....' with arguments 오류 (1) | 2023.11.12 |
url 네임스페이스(app_name) 과 url 별칭. {% url '' , redirect (1) | 2023.11.12 |
장고 템플릿 문법 (0) | 2023.11.12 |
장고 objects 쿼리 조회/ 추가 / 수정 / 답글 / 답글 조회 (0) | 2023.11.11 |
댓글