• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    해결됨

AttributeError 질문드립니다

20.02.02 22:12 작성 조회수 374

0

섹션 13 강의 진행중인데 테스트 중에 에러가 났어요.

    def test_post_list_with_post(self):
        tag_america = create_tag(name='america')

        post_000 = create_post(
            title='The first post',
            content='Hello World. We are the world.',
            author=self.author_000,
        )
        post_000.tags.add(tag_america)
        post_000.save()

        post_001 = create_post(
            title='The second post',
            content='Second Second Second',
            author=self.author_000,
            category=create_category(name='정치/사회')
        )
        post_001.tags.add(tag_america)
        post_001.save()

        self.assertGreater(Post.objects.count(), 0)

        response = self.client.get('/blog/')
        self.assertEqual(response.status_code, 200)
        soup = BeautifulSoup(response.content, 'html.parser')
        body = soup.body
        self.assertNotIn('아직 게시물이 없습니다.', body.text)
        self.assertIn(post_000.title, body.text)

        post_000_read_more_bin = body.find('a', id='read-more-post-{}'.format(post_000.pk))
        self.assertEqual(post_000_read_more_bin['href'], post_000.get_absolute_url())

        self.check_right_side(soup)

        # main_div에는
        main_div = soup.find('div', id='main-div')
        self.assertIn('정치/사회', main_div.text)  # '정치/사회' 있어야 함
        self.assertIn('미분류', main_div.text)  # '미분류' 있어야 함

        # Tag
        post_card_000 = main_div.find('div', id='post-card-{}'.format(post_000.pk))
        self.assertIn('#america', post_card_000.text)  # Tag가 해당 post의 card마다 있다.

사용한 코드를 일부 가져와 봤는데 어디가 잘못된건지 알 수 있을까요?

맨 아랫줄이 line 167 입니다.

답변 2

·

답변을 작성해보세요.

0

루루룽님의 프로필

루루룽

질문자

2020.02.03

빠른고 자세한 답변 감사합니다ㅠㅠ! 정확히 저부분에 큰따옴표가 잘못 찍혀 있었네요ㅠㅠ 덕분에 잘 해결됐어요 감사합니다!

0

NoneType object has no attribute 'text'라는 메세지를 보니, post_card_000 이 None인듯합니다. main_div에서 id가 post-card-{post_000의 pk} 인 div를 찾으려고 했지만, 찾지 못했을 때 이런 메세지가 나올 수 있습니다. HTML 파일에서 post를 카드(div)로 보여주는 곳에 post-card-{p.pk} 가 제대로 되어 있는지 확인해보세요.