Generic detail view ~~ must be called with either an object pk or a slug. 처리 방법

2016. 9. 15. 13:11프로그래밍/Python & dJango


1. generic view 를 사용하여 아래 클레스를 작성

class AuthorDetail(DetailView):
model = Author



2. 위의 코드를 작성한 후에 화면을 출력하면 아래와 같은 에러가 발생하는 경우가 있다.

>> AttributeError: Generic detail view AuthorDetail must be called with either an object pk or a slug.



3. 함수 get_object를 추가해주면 해결된다.

class AuthorDetail(DetailView):
model = Author
template_name = 'books/author_detail.html'

def get_object(self):

object = get_object_or_404(Author, id=self.kwargs['id'])
return object