Inflearn Community Q&A
Field 'id' expected a number but got '미분류'
Written on
·
996
0
강의를 보며 미분류 카테고리를 처리하는 category_page를 만들던 와중에
def category_page(requset, slug):
if slug=='no-category':
category='미분류'
post_list=Post.objects.filter(category=None)
else:
category=Category.objects.get(slug=slug)
post_list = Post.objects.filter(category=category)
return render(
requset,
'blog/index.html',
{
'post_list':Post.objects.filter(category=category),
'categories':Category.objects.all(),
'no_category_post_count':Post.objects.filter(category=None).count(),
'category':category
}
)
ValueError: Field 'id' expected a number but got '미분류'
라는 오류가 뜹니다
해결법을 알 수 있을까요?
dockerawsbootstraptdddjangojavascriptHTML/CSSpython
Answer 3
0
SungYong Lee
Instructor
제가 만든 코드를 보면, if slug=='no_category'로 no와 category 사이에 언더바로 표시가 되어 있습니다.
언더바가 아니라 -를 쓰셨기 때문에 if문으로 들어가지 못하고, else로 빠지는 것 같습니다.
def category_page(request, slug):
if slug == 'no_category':
category = '미분류'
post_list = Post.objects.filter(category=None)
else:
category = Category.objects.get(slug=slug)
post_list = Post.objects.filter(category=category)
return render(
request,
'blog/post_list.html',
{
'post_list': post_list,
'categories': Category.objects.all(),
'no_category_post_count': Post.objects.filter(category=None).count(),
'category': category
}
)
0
newyean62408
Questioner
아 근데 해결이 됐다기엔 애매한게 당연히 category로 미분류라는 문자열이 안넘어가니 category 배지가 뜨질 않네요; 해결 부탁드립니다
0





