검색과정렬에서 오류가 납니다
검색과정렬
검색과 정렬이 안되고 하얀화면만 보입니다
카테고리기능 만들고 나서 그런것 같습니다
baseview.py와 question_list.html에서 코드가 어떻게 달라지는지 알려 주시면 고맙겠습니다
question_view.py파일내용
import logging
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, Sum, IntegerField
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from ..forms import QuestionForm
from ..models import Question, Answer, Category
logger = logging.getLogger('board')
class ListParam:
def __init__(self, page, kw, so):
self.page = page
self.kw = kw
self.so = so
def question_list(request, category_name):
"""
목록출력
"""
# 입력 파라미터
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)
_question_list = _question_list.order_by('-notice', '-create_date')
# 검색
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','-create_date')
elif so == 'popular':
_question_list = _question_list.order_by('-notice','-create_date')
else: # recent
_question_list = _question_list.order_by('-notice','-create_date')
# 페이징처리
paginator = Paginator(_question_list, 10) # 페이지당 10개씩 보여주기
page_obj = paginator.get_page(page)
context = {'category': category, 'question_list': page_obj, 'page': page, 'kw': kw, 'so': so}
return render(request, 'board/question_list.html', context)
#question_list.html파일내용
{% extends 'base.html' %}
{% load board_filter %}
{% block content %}
<div class="container my-3">
<div class="row justify-content-between my-3">
<div class="col-2">
<select class="form-control so">
<option value="recent" {% if so == 'recent' %}selected{% endif %}>최신순</option>
<option value="recommend" {% if so == 'recommend' %}selected{% endif %}>추천순</option>
<option value="popular" {% if so == 'popular' %}selected{% endif %}>인기순</option>
</select>
</div>
<div class="col-4 input-group">
<input type="text" class="form-control kw" value="{{ kw|default_if_none:'' }}">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" id="btn_search">찾기</button>
</div>
</div>
</div>
<table class="table">
<thead>
<tr class="text-center thead-dark">
<th>번호</th>
<th>추천</th>
<th style="width:48%">제목</th>
<th>글쓴이</th>
<th>작성일시</th>
</tr>
</thead>
<tbody>
{% if question_list %}
{% for question in question_list %}
<tr class="text-center"></tr>
<td> {{ question_list.paginator.count|sub:question_list.start_index|sub:forloop.counter0|add:1 }}</td>
<td>
{% if question.voter.all.count > 0 %}
<span class="badge badge-warning px-2 py-1">{{ question.voter.all.count }}</span>
{% endif %}
{% if question.notice == 1 %}
<!---<span class="text-danger small ml-2">{{'공지'}}</span>--->
<span class="text-danger small ml-2"><img src="/static/notice.png"></span>
{% endif %}
</td>
<td class="text-left">
<a href="{% url 'board:detail' question.id %}">{{ question.subject }}</a>
{% if question.answer_set.count > 0 %}
<span class="text-danger small ml-2">{{ question.answer_set.count }}</span>
{% endif %}
</td>
<td>{{ question.author.username }}</td>
<td>{{ question.create_date }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="3">등록된 글이 없습니다.</td>
</tr>
{% endif %}
</tbody>
</table>
<!-- 페이징처리 시작 -->
<!-- 페이징처리 시작 -->
<ul class="pagination justify-content-center">
<!-- 이전페이지 -->
{% if question_list.has_previous %}
<li class="page-item">
<a class="page-link" data-page="{{ question_list.previous_page_number }}">이전</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" tabindex="-1" aria-disabled="true" href="#">이전</a>
</li>
{% endif %}
<!-- 페이지리스트 -->
{% for page_number in question_list.paginator.page_range %}
{% if page_number == question_list.number %}
<li class="page-item active" aria-current="page">
<a class="page-link" data-page="{{ page_number }}" href="#">{{ page_number }}</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" data-page="{{ page_number }}" href="#">{{ page_number }}</a>
</li>
{% endif %}
{% endfor %}
<!-- 다음페이지 -->
{% if question_list.has_next %}
<li class="page-item">
<a class="page-link" data-page="{{ question_list.next_page_number }}" href="#">다음</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" tabindex="-1" aria-disabled="true" href="#">다음</a>
</li>
{% endif %}
</ul>
<!-- 페이징처리 끝 -->
<!-- 페이징처리 끝 -->
<a href="{% url 'board:question_create' category %}" class="btn btn-primary">글 등록하기</a>
</div>
<form id="searchForm" method="get" action="{% url 'index' %}">
<input type="hidden" id="kw" name="kw" value="{{ kw|default_if_none:'' }}">
<input type="hidden" id="page" name="page" value="{{ page }}">
<input type="hidden" id="so" name="so" value="{{ so }}">
</form>
{% endblock %}
{% block script %}
<script type='text/javascript'>
$(document).ready(function(){
$(".page-link").on('click', function() {
$("#page").val($(this).data("page"));
$("#searchForm").submit();
});
$("#btn_search").on('click', function() {
$("#kw").val($(".kw").val());
$("#page").val(1); // 검색버튼을 클릭할 경우 1페이지부터 조회한다.
$("#searchForm").submit();
});
$(".so").on('change', function() {
$("#so").val($(this).val());
$("#page").val(1);
$("#searchForm").submit();
});
});
</script>
{% endblock %}
kmyang 님 768
M 2022년 1월 2일 6:50 오후