박응용님, post_list.html 입니다.

https://pybo.kr/pybo/question/detail/1414/
위 글 글쓴이 입니다.

{% extends 'base.html' %}
{% load posting_filter %}
{% block content %}
{% load static %}
<div class="container my-3">
    <div class="row">
<!-- Sidebar  -->
<nav id="sidebar" class="border-top border-secondary">

</nav>

        <div class="col-sm-12 col-md-3 col-lg-2 p-2">
            <div class="list-group">
                {% for cat in category_list %}
                    {% if cat == category %}
                        <a class="rounded-0 list-group-item list-group-item-action list-group-item-light active"
                           href="{{ cat.get_absolute_url }}">{{ cat.description }}</a>
                    {% else %}
                        <a class="rounded-0 list-group-item list-group-item-action list-group-item-light"
                           href="{{ cat.get_absolute_url }}">{{ cat.description }}</a>
                    {% endif %}
                {% endfor %}
                <a class="rounded-0 list-group-item list-group-item-action list-group-item-light"
                href="{% url 'wyw:ranking' %}">{{ "인기있는 보관함" }}</a>
                {% if user.is_authenticated %}
                <a class="rounded-0 list-group-item list-group-item-action list-group-item-light"
                href="{% url 'account:profile_scrap' user.id %}">{{ "나의 보관함" }}</a>
                <a class="rounded-0 list-group-item list-group-item-action list-group-item-light"
                href="{% url 'account:profile' %}">{{ "프로필 사진 수정" }}</a>
                <a class="rounded-0 list-group-item list-group-item-action list-group-item-light"
                href="{% url 'wyw:recommend' %}">{{ "나에게 맞는 사이트 추천" }}</a>
                {% else %}
                {% endif %}
            </div>

        </div>

        <div class="col-sm-12 col-md-9 col-lg-10 p-2">
<!-- Content -->
<div id="content" class="border-top border-secondary">
    <div class="content_block">
    <h5 class="border-bottom pl-2 pb-3 my-2">{{ category.description }}</h5>


    <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="row my-3">

        <div class="col-6">
            <a href="{% url 'wyw:posting_create' category %}" class="btn btn-primary">글 작성하기</a>
        </div>
        <div class="col-6">
            <div class="input-group">
                <input type="text" id="search_kw" class="form-control" 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>
    </div>
    </div>

    <table class="table">
        <thead>
            <tr class="text-center thead-dark">
                <th>로고</th>
                <th>번호</th>
                <th>추천</th>
                <th style="width:50%">제목</th>
                <th>글쓴이</th>
                <th>작성일시</th>
                <th>평점</th>
            </tr>
        </thead>
        <tbody>
            {% if posting_list %}
            {% for Posting in posting_list %}
            <tr class="text-center">
                <td>    <img class="bd-placeholder-img rounded-circle" height=50 width=50 src="{{Posting.logo}}" alt="">       </td>
                <td>{{ posting_list.paginator.count|sub:posting_list.start_index|sub:forloop.counter0|add:1 }}</td>
                <td>
                    {% if Posting.voter.all.count > 0 %}
                    <span class="badge badge-warning px-2 py-1">{{ Posting.voter.all.count }}</span>
                    {% endif %}
                </td>    
                <td class="test-start">
                    <a href="{% url 'wyw:detail' Posting.id %}">
                        {{ Posting.subject }}
                    </a>
                    {% if Posting.comment_set.count > 0 %}
                    <span class="text-danger small ml-2">
                        {{ Posting.comment_set.count }}
                    </span>
                    {% endif %}
                </td>
                <td>{{ Posting.author.name }}</td>
                <td> {{ Posting.create_date|date:'Y-m-d H' }} </td>
                <td>{{ Posting.user_Rating }}</td>
            </tr>
            {% endfor %}
            {% else %}
            <tr>
                <td colspan="4">게시글이 없습니다.</td>
            </tr>
            {% endif %}
            </tbody>
    </table>

    <!-- 페이징 처리 시작 -->
    <ul class="pagination justify-content-center">
        <!-- 이전 페이지 -->
        {% if posting_list.has_previous %}
        <li class="page-item">
            <a class="page-link" data-page="{{ posting_list.previous_page_number }}"
            href="javascript:void(0)">이전</a>
         </li>
        {% else %}
        <li class="page-item disabled">
            <a class="page-link" tableindex="-1" aria-disabled="true" href="#">
                이전
            </a>
        </li>
        {% endif %}
        <!-- 페이지 리스트 -->
        {% for page_number in posting_list.paginator.page_range %}
        {% if page_number >= posting_list.number|add:-5 and page_number <= posting_list.number|add:5 %}
            {% if page_number == posting_list.number %}
            <li class="page-item active" aria-current="page">
                <a class="page-link" href="?page={{ page_number }}"
                href="javascript:void(0)">{{page_number}}</a>
            </li>
            {% else %}
            <li class="page-item">
                <a class="page-link" href="?page={{page_number}}"
                href="javascript:void(0)">{{ page_number }}</a>
            </li>
            {% endif %}
        {% endif %}
        {% endfor %}
        <!-- 다음 페이지 -->
        {% if posting_list.has_next %}
        <li class="page-item">
            <a class="page-link" data-page="{{ posting_list.next_page_number }}"
                href="javascript:void(0)">다음</a>
        </li>
        {% else %}
        <li class="page-item disabled">
            <a class="page-link" tableindex="-1" aria-disabled="true" href="javascript:void(0)">
                다음
            </a>
        </li>
        {% endif %}
    </ul>
</div>

<form id="searchForm" method="get" action="{% url 'wyw: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>
<div class="container my-3">
    <div class="row">

        <div class="col-sm-12 col-md-3 col-lg-2 p-2">
<!-- Sidebar  -->

{% 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 %}

lgj 317

2022년 8월 13일 7:06 오후

@박응용님 네네 맞아요!! def index(request, category_name='backend'): 즉 카테고리 이름이 기본값으로 backend가 되어있어서 그런지 다른 카테고리 갔다가 콤보박스 변경하면 다시 backend 카테고리로 되돌아와지더라고요... 계속 form action값 확인해봤는데도 계속 안돼서 뭐가뭔지모르겠습니다 ㅠㅠ - lgj님, 2022년 8월 13일 10:35 오후 추천 , 대댓글
@lgj님 searchForm에 카테고리도 추가해야 할것 같네요. - 박응용님, 2022년 8월 13일 10:41 오후 추천 , 대댓글
@박응용님 <input type="hidden" id="category" name="category" value="{{ category }}"> 로 추가했었는데 안돼요 ㅠㅠ - lgj님, 2022년 8월 13일 11:52 오후 추천 , 대댓글
@박응용님 def index(request, category_name='backend'): 이게 views.py의 index 함수인데 category_name으로 파라미터를 전송하는 방법이 있을까요? - lgj님, 2022년 8월 13일 11:57 오후 추천 , 대댓글
@lgj님 action="{% url 'wyw:index' 'backend' %}" 처럼 전달할수 있습니다. - 박응용님, 2022년 8월 14일 1:07 오전 추천 , 대댓글
목록으로