ํ๋ก ํธ ์์ ์ ์งํํ๋ ์ค ๊ฒ์๊ธ ๋ชฉ๋ก๋ค์ด ๋ณด์ด๋ ๋ฉ์ธ ํ์ด์ง์์ ํน์ ๊ฒ์๊ธ์ ์ ํํ์ ๋ ํด๋น ๊ฒ์๊ธ์ ์์ธํ์ด์ง๋ก ์ด๋ํ๊ณ , ์์ธ ํ์ด์ง์์๋ ํด๋น ๊ฒ์๊ธ์ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ์ ์๊ฒฉ API๋ฅผ ํธ์ถํ์ฌ ๊ฐ์ ธ์์ผํ๋ค.
๊ทธ๋ฌ๊ธฐ ์ํด์๋ ๋ฐฑ์๋์์ url์ ์ค๊ณํ๋๋ก ์๋์ ๊ฐ์ด ๊ฒ์๊ธ์ id ๊ฐ์ url ํ๋ผ๋ฏธํฐ๋ก ๊ฐ์ด ์ ๋ฌ ํด์ฃผ์ด์ผ ํ๋ค.
# articles/urls.py
urlpatterns = [
path('', views.ArticleView.as_view(), name='article_view'),
path('<int:article_id>/', views.ArticleDetailView.as_view(),
]
๋จผ์ JavaScript์ fetch ์์ฒญ์ ๋ณด๋ด๋ ํจ์๋ฅผ ์คํํ์ฌ ๊ฒ์๊ธ ๋ชฉ๋ก๋ค์ response๋ฅผ ๋ฐ์์ค๊ณ ๋ฐ๋ณต๋ฌธ์ ๋๋ ค ๊ฒ์๊ธ์ ์์ฑํ ๋ onclick ์์ฑ์ ๋ถ์ฌํด์ ์คํ๋ ํจ์ ์์ id ๊ฐ์ ๋ฃ์ด์ฃผ์๋ค.
async function article_list() {
const response = await get_articles()
const data = await response.json()
for (let i = 0; i < data.length; i++) {
let id = data[i]['id']
let title = data[i]['title']
let comment = data[i]['comment_article']
let category = data[i]['category']
let user = data[i]['user']
let date = data[i]['date']
let time = data[i]['time']
let like = data[i]['like']
let views = data[i]['views']
const article = `<tr>
<td>${id}</td>
<td>${category}</td>
<td align="left" id="article_id"token interpolation">${id})">${title} [${comment}]</td>
<td>${user}</td>
<td>${date}</td>
<td>${time}</td>
<td>${like}</td>
<td>${views}</td>
</tr>`
const article_list = document.getElementById('article_list')
article_list.insertAdjacentHTML('beforeend', article)
}
๊ทธ ๋ค์ ํด๋น ๊ฒ์๊ธ์ ๋๋ ์ ๋ id๊ฐ์ ๋ฐ์ ๊ฒ์๊ธ ์์ธ ํ์ด์ง๋ก ์ด๋ํ๋ ํจ์๊ฐ ์คํ๋๊ณ ํจ์๊ฐ ์คํ๋๋ฉด์ ์ ๋ฌ๋ฐ์ id ๊ฐ์ url ํ๋ผ๋ฏธํฐ๋ก ๋ฃ์ด์ค๋ค.
function replace_article_detail(article_id) {
const url = `${frontend_base_url}/templates/main/article_detail.html?id=${article_id}`;
location.href = url;
}
๊ทธ๋ฆฌ๊ณ ์๋์ ๊ฐ์ด ๋ฐฑ์๋ ์๋ฒ์ ์๊ฒฉ API๋ฅผ ํธ์ถํ์ฌ ๋ฐ์ดํฐ๋ฅผ response๋ก ๋ฐ์์ค๋ ํจ์๋ฅผ ๋ง๋ค์ด ์ค ๋ค์
async function get_article_detail(article_id){
const response = await fetch(`${backend_base_url}/articles/${article_id}/`, {
method: "GET",
})
return response
}
๊ฒ์๊ธ์ ์์ธ ํ์ด์ง๋ก ์ด๋ํ๊ฒ ๋๋ฉด ์์์ ๋ง๋ ํน์ ๊ฒ์๊ธ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐฑ์๋ ์๋ฒ์ ์๊ฒฉ API๋ฅผ ํธ์ถํ๋ ํจ์๊ฐ ์คํ๋๋๋ฐ ์ด ๋ ๋ค์ด๊ฐ id ๊ฐ์ ์ด๋ป๊ฒ ๋ฃ์ด์ค์ผ ํ ์ง ์ ๋ง ๊ณ ๋ฏผ์ ๋ง์ด ํ๋ค.
async function LoadDeatail(article_id) {
// ๋ฐฑ์๋ ์๋ฒ์ ํด๋น ๊ฒ์๊ธ์ id ๊ฐ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๋ ํจ์
const response = await get_article_detail(article_id)
const data = await response.json()
const title = document.getElementById('article_title')
title.innerText = data['title']
const author = document.getElementById('author_info')
author.innerText = data['user'] + data['date'] + data['time']
const views = document.getElementById('views')
views.innerText = '์กฐํ์ : ' + data['views']
const likes = document.getElementById('likes')
likes.innerText = '์ถ์ฒ์ : ' + data['like']
const article_image = document.getElementById('article_image')
article_image.src = `${backend_base_url}`+data['article_image']
// article_image.setAttribute('src', `${backend_base_url}`+data['article_image'])
const content = document.getElementById('content')
content.innerText = data['content']
}
๊ตฌ๊ธ๋ง์ ํ๋ฉด์ ๋ฐฉ๋ฒ์ ์ฐพ๋ ์ค window.location.search๋ก ํ์ฌ ์์น์ url์์ Parameter ๊ฐ์ ์ป์ ์ ์๋ค๋ ๊ฒ์ ์๊ฒ๋์๊ณ , new URLSearchParams()๋ก URLSearchParams๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ค์ .get()์ผ๋ก ํด๋น ๊ฐ์ฒด์์ Parameter๋ฅผ ์ถ์ถ ํ ์ ์์๋ค.
// ํ์ฌ URL์ ์ฟผ๋ฆฌ์คํธ๋ง ๊ฐ์ ๊ฐ์ ธ์ด -> ex)?id=1
const url_str = window.location.search
// url_str์ URLSearchParams ๊ฐ์ฒด๋ฅผ ์์ฑ
const urlParams = new URLSearchParams(url_str);
// URLSearchParams ๊ฐ์ฒด์์ id ๊ฐ๋ง ์ถ์ถ -> ex)1
const article_id = urlParams.get("id");