작성
·
245
0
이제 막 공부를 시작한터라 몇일째 혼자 해결해볼려고 해도 도저히 모르겠습니다...
전 강의 tag페이지 만들기에선 아무런 문제가없었는데 post detail개선 부분부터 오류가 나옵니다.
이 오류를 무시하고 그대로 진도를 나가도 블로그 나 어드민 페이지에선 아무런 문제가 발생하진않지만 어느부분이 잘못되어서 오류가 나오는지 가르쳐주세요!ㅠㅠ
post_detail
{% extends 'blog/base.html' %}
{% block content %}
<h1 id="blog-list-title">
Blog
{% if category %}<small class="text-muted">: {{ category }}</small>{% endif %}
{% if tag %}<small class="text-muted">: #{{ tag }}</small>{% endif %}
</h1>
{% if object_list.exists %}
{% for p in object_list %}
<!-- Blog Post -->
<div class="card mb-4" id="post-card-{{ p.pk }}">
{% if p.head_image %}
<img class="card-img-top" src="{{ p.head_image.url }}" alt="Card image cap">
{% else %}
<img class="card-img-top" src="https://picsum.photos/750/300/?random" alt="Card image cap">
{% endif %}
<div class="card-body">
{% if p.category %}
<span class="badge badge-primary float-right">{{ p.category }}</span>
{% else %}
<span class="badge badge-primary float-right">미분류</span>
{% endif %}
<h2 class="card-title">{{ p.title }}</h2>
<p class="card-text">{{ p.content | truncatewords:50 }}</p>
{% for tag in p.tags.all %}
<a href="{{ tag.get_absolute_url }}">#{{ tag }}</a>
{% endfor %}
<br/>
<br/>
<a href="{{ p.get_absolute_url }}" class="btn btn-primary" id="read-more-post-{{ p.pk }}">Read More →</a>
</div>
<div class="card-footer text-muted">
Posted on {{ p.create }} by
<a href="#">{{ p.authro }}</a>
</div>
</div>
{% endfor %}
{% else %}
<h3>아직 게시물이 없습니다.</h3>
{% endif %}
{% endblock %}
test.py
from django.test import TestCase, Client
from bs4 import BeautifulSoup
from .models import Post, Category, Tag
from django.utils import timezone
from django.contrib.auth.models import User
def create_category(name='life', description=''):
category, is_created = Category.objects.get_or_create(
name=name,
description=description
)
category.slug = category.name.replace(' ', '-').replace('/', '')
category.save()
return category
def create_tag(name='some tag'):
tag, is_created = Tag.objects.get_or_create(
name=name
)
tag.slug = tag.name.replace(' ', '-').replace('/', '')
tag.save()
return tag
def create_post(title, content, author, category=None):
blog_post = Post.objects.create(
title=title,
content=content,
created=timezone.now(),
author=author,
category=category,
)
return blog_post
class TestModel(TestCase):
def setUp(self):
self.client = Client()
self.author_000 = User.objects.create(username='smith', password='nopassword')
def test_category(self):
category = create_category()
def test_post(self):
category = create_category()
post_000 = create_post(
title='The first post',
content='Hello World. We are the world',
author=self.author_000,
category=category,
)
self.assertEqual(category.post_set.count(), 1)
def test_tag(self):
tag_000 = create_tag(name='project') # bad_guy
tag_001 = create_tag(name='portfolio') # 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_000)
post_000.tags.add(tag_001)
post_000.save()
post_001 = create_post(
title='Stay Fool, Stay Hungry',
content='Story about Steve jobs',
author=self.author_000
)
post_001.tags.add(tag_001)
post_001.save()
self.assertEqual(post_000.tags.count(), 2) # post 는 여러개의 tag를 가질수잇다
self.assertEqual(tag_001.post_set.count(), 2) # 하나의 Tag는 여러개의 post에 붙을수있다
self.assertEqual(tag_001.post_set.first(), post_000) # 하나의 Tag는 자신을 가진 post들을 불러올수있다
self.assertEqual(tag_001.post_set.last(), post_001) # 하나의 Tag는 자신을 가진 post들을 불러올수있다
def test_post(self):
category = create_category()
post_000 = create_post(
title='The first post',
content='Hello World. We are the world',
author=self.author_000,
category=category,
)
class TestView(TestCase):
def setUp(self):
self.client = Client()
self.author_000 = User.objects.create(username='smith', password='nopassword')
def check_navbar(self, soup):
navbar = soup.find('div', id='navbar')
self.assertIn('Blog', navbar.text)
self.assertIn('About me', navbar.text)
def check_right_side(self, soup):
category_card = soup.find('div', id='category-card')
self.assertIn('미분류 (1)', category_card.text)
self.assertIn('정치/사회 (1)', category_card.text)
def test_post_list_no_post(self):
response = self.client.get('/blog/')
self.assertEqual(response.status_code, 200)
soup = BeautifulSoup(response.content, 'html.parser')
title = soup.title
self.assertIn(title.text, 'Blog')
self.check_navbar(soup)
self.assertEqual(Post.objects.count(), 0)
self.assertIn('아직 게시물이 없습니다', soup.body.text)
def test_post_list_with_post(self):
tag_portfolio = create_tag(name='portfolio')
post_000 = create_post(
title='The first post',
content='Hello World. We are the world',
author=self.author_000,
)
post_000.tags.add(tag_portfolio)
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_portfolio)
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_btn = body.find('a', id='read-more-post-{}'.format(post_000.pk))
self.assertEqual(post_000_read_more_btn['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('#portfolio', post_card_000.text) # Tag 가 해당 post의 card 마다 있다.
def test_post_detail(self):
category_politics = create_category(name='정치/사회')
post_000 = create_post(
title='The first post',
content='Hello World. We are the world',
author=self.author_000,
category=category_politics
)
tag_portfolio = create_tag(name='portfolio')
post_000.tags.add(tag_portfolio)
post_000.save()
post_001 = create_post(
title='The second post',
content='Second Second Second',
author=self.author_000,
category=create_category(name='정치/사회')
)
self.assertGreater(Post.objects.count(), 0)
post_000_url = post_000.get_absolute_url()
self.assertEqual(post_000_url, '/blog/{}/'.format(post_000.pk))
response = self.client.get(post_000_url)
self.assertEqual(response.status_code, 200)
soup = BeautifulSoup(response.content, 'html.parser')
title = soup.title
self.assertEqual(title.text, '{} - Blog'.format(post_000.title))
self.check_navbar(soup)
body = soup.body
main_div = body.find('div', id='main-div')
self.assertIn(post_000.title, main_div.text)
self.assertIn(post_000.author.username, main_div.text)
self.assertIn(post_000.content, main_div.text)
self.check_right_side(soup)
# Tag
self.assertIn('#portfolio', main_div.text)
self.assertIn(category_politics.name, main_div.text)# 카테고리가 main_div 에 있다.
self.assertNotIn('EDIT', main_div.text)# 에디트 버튼이 로그인하지 않은유저에겐 보이지않는다
login_success = self.client.login(username='smith', password='nopassword')# 로그인을 한 경우에는
self.assertTrue(login_success)
response = self.client.get(post_000_url)
self.assertEqual(response.status_code, 200)
soup = BeautifulSoup(response.content, 'html.parser')
main_div = soup.find('div', id='main-div')
self.assertEqual(post_000.author, self.author_000)# post.author 와 login 한 사용자가 동일하면
self.assertIn('EDIT', main_div)# EDIT 버튼이 있다.
# 다른 사람인 경우에는 없다.
def test_post_list_by_category(self):
category_politics = create_category(name='정치/사회')
post_000 = create_post(
title='The first post',
content='Hello World. We are the world',
author=self.author_000,
)
post_001 = create_post(
title='The second post',
content='Second Second Second',
author=self.author_000,
category=category_politics
)
response = self.client.get('/blog/category/_none/')
self.assertEqual(response.status_code, 200)
soup = BeautifulSoup(response.content, 'html.parser')
# self.assertEqual('Blog - {}'.format(category_politics.name), soup.title.text)
main_div = soup.find('div', id='main-div')
self.assertIn('미분류', main_div.text)
self.assertNotIn(category_politics.name, main_div.text)
def test_teg_page(self):
tag_000 = create_tag(name='project') # bad_guy
tag_001 = create_tag(name='portfolio') # 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_000)
post_000.tags.add(tag_001)
post_000.save()
post_001 = create_post(
title='Stay Fool, Stay Hungry',
content='Story about Steve jobs',
author=self.author_000
)
post_001.tags.add(tag_001)
post_001.save()
response = self.client.get(tag_000.get_absolute_url())
self.assertEqual(response.status_code, 200)
soup = BeautifulSoup(response.content, 'html.parser')
main_div = soup.find('div', id='main-div')
blog_h1 = main_div.find('h1', id='blog-list-title')
self.assertIn('#{}'.format(tag_000.name), blog_h1.text)
self.assertIn(post_000.title, main_div.text)
self.assertNotIn(post_001.title, main_div.text)
답변 2
1
fail이 된 이유를 알아보기 위해 에러메시지를 잘 읽어보면 "미분류 (1)"이 안 보인다고 되어 있습니다.
대신 미분류(0)이 되어 있다고 하네요.
그래서 테스트코드를 보니, post_000과 post_001 모두에 '정치/사회'라는 name을 갖는 tag를 추가해 놓으셨습니다.
제 영상에서 2:51을 확인해보세요. post_001은 category를 설정하지 않습니다.
0