1. Question 모델의 id 값을 이용하여 질문내용(views.detail) 출력하기
/* pybo/url.py */
from django.urls import path
form . import views
urlpattern = [
path('',views.index),
path('<int:question_id>/',views.detail),
]
- /pybo/2/가 요청 => views.detail 함수 실행.
2. 질문내용 출력 함수(views.detail) 작성하기
/*----- 생략 -----*/
def detail(request, question_id):
"""
질문내용 출력하기
"""
question = Question.objects.get(id=question_id)
context = {'question': question}
return render(request, 'pybo/question_detail.html', context)
3. 질문내용출력 템플릿 만들기
<h1>{{ question.subject }}</h1>
<div>
{{ question.content }}
</div>
- question_detail.html 을 tamplates/pybo 폴더 내부에 만든다.
- localhost:8000/pybo/2/ 에 접속하면 질문내용을 확인할 수 있다.
4. 404Page 출력하기
- 사용자가 주소창에 임의의 question_id 를 입력했을때 pk 값이 없으면 장고는 오류페이지를 부르게된다.
- 오류페이지 대신 404 페이지를 출력하도록 detail 함수를 수정할 수 있다
from django.shortcuts import render, get_object_or_404
/* --- 생략 --- */
def detail(request, question_id):
"""
질문내용 출력하기
"""
question = get_object_or_404(Question, pk=question_id)
context = {'question': question}
return render(request, 'pybo/question_detail.html', context)
위 문서는 이지스퍼블리싱에서 출간한 박응용님의 "점프투 장고"를 제가 공부하면서 요약한 내용입니다.
게시한 내용은 공부를 하면서 저만 알아볼 수 있게 요약한 부분들도 많으므로 부족한 내용은
직접 책을 구입하셔서 보시면 좋을 것 같습니다.
'Study > django' 카테고리의 다른 글
django - Answerform (0) | 2021.09.25 |
---|---|
django - Namespace (0) | 2021.09.25 |
django - QuestionTitleList (0) | 2021.09.25 |
django - Admin (0) | 2021.09.25 |
django - DataControl (0) | 2021.09.25 |