404 오류 처리

Python/django 2020. 8. 22. 23:15

Http404 를 처리할 때는 loader-render 관계처럼 단축 함수가 존재 한다.

 

get_object_or_404

 

from django.shortcuts import render, get_object_or_404

def detail(request, question_id):
    # try:
    #     question = Question.objects.get(pk=question_id)
    # except Question.DoesNotExist:
    #     raise Http404("Question does not exist")
    
    question = get_object_or_404(Question, pk=question_id)
    # return HttpResponse("You're looking at question %s." % question_id)
    return render(request, 'polls/detail.html', {'question': question})

 

'Python > django' 카테고리의 다른 글

간단한 게시판 구현하기  (0) 2021.01.04
하드 코딩 URL 없애기  (0) 2020.08.23
모델 만들기  (0) 2020.08.22
뷰 만들기  (0) 2020.08.22
프로젝트 구조  (1) 2020.07.05
블로그 이미지

hjc_

୧( “̮ )୨

,