raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'image' attribute has no file associated with it.
위와 같은 에러가 나옵니다.
*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='대외활동', 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, organization, author, link, category=None):
blog_post = Post.objects.create(
category=category,
title=title,
organization=organization,
birthline=timezone.now(),
deadline=timezone.now(),
link=link,
author=author,
created=timezone.now(),
updated=timezone.now(),
)
return blog_post
class TestModel(TestCase):
def setUp(self):
self.client = Client()
self.author_000 = User.objects.create(
username='westline', password='nopassword')
def test_category(self):
category = create_category()
post_000 = create_post(
title='The first post',
organization='SK',
link='https://www.naver.com',
author=self.author_000,
category=category,
)
self.assertEqual(category.post_set.count(), 1)
def test_tag(self):
tag_000 = create_tag(name='bad_guy')
tag_001 = create_tag(name='america')
post_000 = create_post(
title='The first post',
organization='SK',
link='https://www.naver.com',
author=self.author_000,
)
post_000.tags.add(tag_000)
post_000.tags.add(tag_001)
post_000.save()
post_001 = create_post(
title='The second post',
organization='Samsung',
link='https://www.naver.com',
author=self.author_000,
)
post_001.tags.add(tag_001)
post_001.save()
self.assertEqual(post_000.tags.count(), 2) # post는 여러개의 tag를 가질 수 있다.
# 하나의 tag는 여러개의 post에 붙을 수 있다
self.assertEqual(tag_001.post_set.count(), 2)
self.assertEqual(tag_001.post_set.first(), post_000)
# 하나의 tag는 자신으 가진 post들을 불러올 수 있다.
self.assertEqual(tag_001.post_set.last(), post_001)
def test_post(self):
category = create_category()
post_000 = create_post(
title='The first post',
organization='SK',
link='https://www.naver.com',
author=self.author_000,
category=category,
)
class TestView(TestCase):
def setUp(self):
self.client = Client()
self.author_000 = User.objects.create(
username='westline', password='nopassword')
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, '대외활동의 모든 것')
self.assertEqual(Post.objects.count(), 0)
self.assertIn('아직 게시물이 없습니다.', soup.body.text)
def test_post_list_with_post(self):
post_000 = create_post(
title='The first post',
organization='SK',
link='https://www.naver.com',
author=self.author_000,
)
post_001 = create_post(
title='The second post',
organization='SAMSUNG',
link='https://www.daum.net',
author=self.author_000,
)
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)