점프투장고 '목록으로' 기능구현 관련 오류 Object of type 'ListParam' is not JSON serializable
목록으로 기능을 구현하려
question view에서 아래와 같이 lp를 호출하려하니 Object of type 'ListParam' is not JSON serializable 오류가 납니다.
처리를 위한 별도 작업이 필요한건지 궁금합니다.
class ListParam:
def init(self, page, kw, so):
self.page = page
self.kw = kw
self.so = so
def question_list(request, category_name):
lp = ListParam(page, kw, so)
request.session['lp'] = lp
항상 감사드립니다.
장고가좋아요 님 672
2021년 10월 21일 10:14 오후
1개의 답변이 있습니다. 1 / 1 Page
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.db import transaction
from django.db.models import Q, Count
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from tools.utils import get_client_ip
from ..forms import QuestionForm
from ..models import Question, Answer, Category, QuestionCount
class ListParam:
def __init__(self, page, kw, so):
self.page = page
self.kw = kw
self.so = so
def question_list(request, category_name):
"""
pybo 목록출력
"""
page = request.GET.get('page', '1') # 페이지
kw = request.GET.get('kw', '') # 검색어
so = request.GET.get('so', 'recent') # 정렬기준
lp = ListParam(page, kw, so)
request.session['lp'] = lp
category = get_object_or_404(Category, name=category_name)
_question_list = Question.objects.filter(category__name=category.name)
#검색
if kw:
_question_list = _question_list.filter(
Q(subject__icontains=kw) | # 제목검색
Q(content__icontains=kw) | # 내용검색
Q(author__username__icontains=kw) | # 질문 글쓴이검색
Q(answer__content__icontains=kw) | # 답변내용검색
Q(answer__author__username__icontains=kw) # 답글 글쓴이검색
).distinct()
#정렬
_question_list = _question_list.annotate(
num_voter=Count('voter', distinct=True)+Count('answer__voter', distinct=True),
num_answer=Count('answer', distinct=True)+Count('comment', distinct=True)+Count('answer__comment', distinct=True))
if so == 'recommend':
_question_list = _question_list.order_by('-notice', '-num_voter', '-create_date')
elif so == 'popular':
_question_list = _question_list.order_by('-notice', '-num_answer', '-create_date')
else: # recent
_question_list = _question_list.order_by('-notice', '-create_date')
#페이징처리
paginator = Paginator(_question_list, 3) # 페이지당 25개씩 보여주기 [][][]
page_obj = paginator.get_page(page)
context = {'category': category, 'question_list': page_obj, 'page': page, 'kw': kw, 'so': so}
return render(request, 'pybo/question_list.html', context)
@transaction.atomic
def question_detail(request, question_id):
"""
pybo 내용 출력
"""
page = request.GET.get('page', '1') # 페이지
so = request.GET.get('so', 'recommend') # 답변정렬 (추천순: recommend, 최신순: recent)
question = get_object_or_404(Question, pk=question_id)
lp = request.session.get('lp')
#조회수
ip = get_client_ip(request)
cnt = QuestionCount.objects.filter(ip=ip, question=question).count()
if cnt == 0:
qc = QuestionCount(ip=ip, question=question)
qc.save()
if question.view_count:
question.view_count += 1
else:
question.view_count = 1
question.save()
#답변 정렬
answer_list = Answer.objects.filter(question=question).annotate(num_voter=Count('voter'))
if so == 'recommend':
answer_list = answer_list.order_by('-num_voter', 'create_date')
else: # 시간순으로 정렬
answer_list = answer_list.order_by('create_date', '-num_voter')
paginator = Paginator(answer_list, 10) # 페이지당 10개씩 보여주기
page_obj = paginator.get_page(page)
context = {'question': question, 'answer_list': page_obj, 'page': page,
'so': so, 'category': question.category, 'lp': lp}
return render(request, 'pybo/question_detail.html', context)
장고가좋아요 님
M 2021년 10월 23일 6:49 오후