앞서 Question 모델과 Anwser 모델에 author 필드를 추가했다. 질문목록, 질문상세 화면에 author 필드를 이용하여 글쓴이 필드를 추가해 보자.
질문목록 화면에 글쓴이 필드 추가
(1단계) 질문목록 템플릿 수정하기
글쓴이를 표시하기 위해 테이블 헤더에 글쓴이 항목을 추가하고, for문에도 글쓴이를 적용한다.
# D:\projects\mysite\templates\pybo\question_list.html {% extends 'base.html' %} {% load pybo_filter %} {% block content %} <div class="container my-3"> <table class="table"> <thead> <tr class="text-center thead-dark"> <th>번호</th> <th style="width:50%">제목</th> <th>글쓴이</th> <th>작성일시</th> </tr> </thead> <tbody> {% if question_list %} {% for question in question_list %} <tr class="text-center"> <td>{{ question_list.paginator.count|sub:question_list.start_index|sub:forloop.counter0|add:1 }}</td> <td class="text-left"> <a href="{% url 'pybo:detail' question.id %}"> {{ question.subject }} </a> <!-- 답변 개수 표시 --> {% if question.answer_set.count > 0 %} <span class="text-danger small ml-2"> {{ question.answer_set.count }} </span> {% endif %} </td> <td>{{ question.author.username }}</td> <!-- 글쓴이 추가 --> <td>{{ question.create_date }}</td> </tr> {% endfor %} {% else %} <tr> <td colspan="3">질문이 없습니다. </td> </tr> {% endif %} </tbody> </table> (...생략...) </div> {% endblock %} |
<td>{{ question.author.username }}</td>를 삽입하여 질문의 글쓴이를 표시했다. 변경된 질문목록을 확인해 보자.
질문상세 화면에 글쓴이 필드 추가
(1단계) 질문 상세 템플릿 수정 및 답변에 글쓴이 표시
질문상세 화면에도 글쓴이를 추가하고, 답변 영역에도 글쓴이를 추가한다.
# D:\projects\mysite\templates\pybo\question_detail.html {% extends 'base.html' %} {% block content %} <div class="container my-3"> <h2 class="border-bottom py-2">{{ question.subject }}</h2> <div class="card my-3"> <div class="card-body"> <div class="card-text" style="white-space: pre-line;"> {{ question.content }} </div> <div class="d-flex justify-content-end"> <div class="badge badge-light p-2 text-left"> <div class="mb-2">{{ question.author.username }}</div> <!--질문상세 글쓴이 추가--> <div>{{ question.create_date }}</div> </div> </div> </div> </div> <h5 class="border-bottom my-3 py-2"> {{ question.answer_set.count }} 개의 답변이 있습니다. </h5> {% for answer in question.answer_set.all %} <div class="card my-3"> <div class="card-body"> <div class="card-text" style="white-space: pre-line;"> {{ answer.content }} </div> <div class="d-flex justify-content-end"> <div class="badge badge-light p-2 text-left"> <div class="mb-2">{{ answer.author.username }}</div> <!--답변 글쓴이 추가--> <div>{{ answer.create_date }}</div> </div> </div> </div> </div> {% endfor %} <form action="{% url 'pybo:answer_create' question.id %}" method="post" class="my-3"> {% csrf_token %} <!-- 오류 표시 Start --> {% if form.errors %} <div class="alert alert-danger" role="alert"> {% for field in form %} {% if field.errors %} <strong>{{ field.label }}</strong> {{ field.errors }} {% endif %} {% endfor%} </div> {% endif %} <!-- 오류 표시 End --> <div class="form-group"> <textarea name="content" id="content" {% if not user.is_authenticated %} disabled {% endif %} class="form-control" rows="10"></textarea> </div> <input type="submit" value="답변등록" class="btn btn-primary"> </form> </div> {% endblock %} |
글쓴이와 작성일시가 함께 보이도록 수정했다. 그리고 부트스트랩을 이용하여 여백과 정렬도 살짝 변경했다.
여기까지.
댓글