๐Ÿ“’ [ TIL ] 2022.07.19_64์ผ์ฐจ # ์ตœ์ข… ํ”„๋กœ์ ํŠธ (6)

๋ฌธ๋ช…์ฃผยท2022๋…„ 7์›” 20์ผ
1
post-thumbnail

[ 2022-07-19 (ํ™”) ์˜ค๋Š˜์˜ TIL ]

[ Today Project ]

drf ๋ฐฑ์—”๋“œ๊ฐœ๋ฐœ + ํ”„๋ก ํŠธ๊ฐœ๋ฐœ
: ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” ํŒ€ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

[ Today Learn ]

  • ๊ฒŒ์‹œ๋ฌผ ์—ฐ๋™
  • ๋กœ๊ทธ์ธ์‹œ ๋กœ๊ทธ์ธ ๋ฐ ํšŒ์›๊ฐ€์ž…๋ฒ„ํŠผ ์ œ๊ฑฐ

โœ๏ธ ๋‚ด๊ฐ€ ๋ฐฐ์šด๊ฒƒ, ์–ป์€๊ฒƒ

views.py

# ๊ฒŒ์‹œํŒ๋ณ„ ์•„ํ‹ฐํด ๋ฆฌ์ŠคํŒ…
class ArticleByBoard(APIView):

    def get(self, request):
        boards = request.query_params.getlist('boards', '')
        print(boards)
        results = []
        for board in boards:
            articles = ArticleModel.objects.filter(board__name=board).order_by("-id")[:5]
            result = ArticleSerializer(articles, many=True).data
            results_data = {
                f"{board}" : result
            }
            results.append(results_data)
        return Response(results)

main.js

// ๊ฒŒ์‹œํŒ๋ณ„ ๊ฒŒ์‹œ๊ธ€ ๋ฆฌ์ŠคํŒ…
myData().then((data) => {
        detail = response_json
        console.log(detail)
        try {       
        for (let i = 0; i < 5; i++) {
            let title = detail[0]['์ง์žฅ์ธ'][i]['article_title']
            let id = detail[0]['์ง์žฅ์ธ'][i]['id']
            let temp_html = `
                <div class="board-title" id="worker"token interpolation">${frontend_base_url}/detail.html?id=${id}'">${title}
            </div>`
            
            $('#company-box').prepend(temp_html)
        }} catch (err) {
        }
        try{
        for (let i = 0; i < 5; i++) {
            let title = detail[1]['๋Œ€ํ•™์ƒ'][i]['article_title']
            let id = detail[1]['๋Œ€ํ•™์ƒ'][i]['id']
            let temp_html = `
                <div class="board-title" id="worker"token interpolation">${frontend_base_url}/detail.html?id=${id}'">${title}
            </div>`
            
            $('#college-box').prepend(temp_html)
        }} catch (err) {}
        try{
        for (let i = 0; i < 5; i++) {
            let title = detail[2]['10๋Œ€'][i]['article_title']
            let id = detail[2]['10๋Œ€'][i]['id']
            let temp_html = `
                <div class="board-title" id="worker"token interpolation">${frontend_base_url}/detail.html?id=${id}'">${title}
            </div>`
            
            $('#teen-box').prepend(temp_html)
        }} catch (err) {}
        try{
        for (let i = 0; i < 5; i++) {
            let title = detail[3]['LGBTQ'][i]['article_title']
            let id = detail[3]['LGBTQ'][i]['id']
            let temp_html = `
                <div class="board-title" id="worker"token interpolation">${frontend_base_url}/detail.html?id=${id}'">${title}
            </div>`
            
            $('#lgbtq-box').prepend(temp_html)
        }} catch (err) {}
        try {
        for (let i = 0; i < 5; i++) {
            let title = detail[4]['์ž์œ '][i]['article_title']
            let id = detail[4]['์ž์œ '][i]['id']
            let temp_html = `
                <div class="board-title" id="worker"token interpolation">${frontend_base_url}/detail.html?id=${id}'">${title}
            </div>`
            
            $('#free-box').prepend(temp_html)
        }} catch (err) {}
        try {
        for (let i = 0; i < 5; i++) {
            let title = detail[5]['HOT'][i]['article_title']
            let id = detail[5]['HOT'][i]['id']
            let temp_html = `
                <div class="board-title" id="worker"token interpolation">${frontend_base_url}/detail.html?id=${id}'">${title}
            </div>`
            
            $('#hot-box').prepend(temp_html)
        }} catch (err) {}

// ๋กœ๊ทธ์ธ ๋กœ๊ทธ์•„์›ƒ ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ ์ˆจ๊ธฐ๊ธฐ
    if (!localStorage.getItem("access")) {
        let logout_button = document.getElementById("logout-button")
        logout_button.style.visibility = "hidden"
    }
    else {
        let login_button = document.getElementById("login-button")
        let signup_button = document.getElementById("signup-button")
        login_button.style.visibility = "hidden"
        signup_button.style.visibility = "hidden"
    }

    const myData = async () => {
        const response = await fetch(`${backend_base_url}/article/board/?boards=์ง์žฅ์ธ&boards=๋Œ€ํ•™์ƒ&boards=10๋Œ€&boards=LGBTQ&boards=์ž์œ &boards=HOT`, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
        
        }
    )
profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 7์›” 20์ผ

์•„์ฃผ ์ž˜ ์„ฑ์žฅํ•˜๊ณ  ์žˆ๊ตฌ๋งŒ ๋ช…์ฃผ์ฟค

1๊ฐœ์˜ ๋‹ต๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด