본문으로 바로가기

인기글 Serializer 생성 | js.empty하고 append하기 |

category TIL 2022. 11. 21. 17:22

인기글에서는 모든게시판의 일정 추천조건을 만족한 게시글이보이는데 익명게시판에서 적은 이름은 보이지 않아야하는데 보이는 에러?  가있어서 시리얼라이저로 조건을 만들어서 보여주면 될 것 같아서 수정했다. 적용하니 바로 오류가 잡힌걸 알 수 있었다. 

아래의 js에서 success empty를 해주고 아래에서 append를 해주면서 사용자가 원하는 카테고리를 눌렀을때 카테고리의 글만 보여지게끔 만들 수 있었다.

$('#blind_list').click( function() {
    blind_list()
});
function blind_list() {
    let category = 'blind' ;
    console.log(category)
    $.ajax({

        type: "GET",
        url: `http://127.0.0.1:8000/blind/category/?category=${category}`,
        data: {},

        headers: {
          "Authorization": "Bearer " + localStorage.getItem("access"),
        },

        success: function (response) {
        $('#post_list').empty()

        console.log('성공:', response);
        if (response.length > 0) {
            for (let i = 0; i < response.length; i++) {
                let id = response[i]['id']

                console.log(id)
                let title = response[i]['title']
                let hits =response[i]['hits']
                let created_at = response[i]['created_date']
                let category = response[i]['category']
                let user = response[i]['user']

                temp_html=` <tr>
                <td>${id}</td>
                <td>
                    <onclick=q(${id})>${title}</a>
                </td>
                <td>${category}</td>
                <td>${hits}</td>
                <td>${user}</td>
                <td>${created_at}</td>
            </tr>`
            $('#post_list').append(temp_html)
        }
        }


        }

      });
}