drf 백엔드개발 + 프론트개발
: 이번 프로젝트는 팀프로젝트로 시작합니다.
* 게시글 수정 및 삭제
api.js
// article 수정
async function updateArticle(contents) {
let updateData = {
article_contents: contents,
}
let response = await fetch(`${backend_base_url}/article/${obj_id}/`, {
method: 'PUT',
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + localStorage.getItem("access"),
"access-control-allow-origin": "*"
},
body: JSON.stringify(updateData)
})
response_json = await response.json()
// article 삭제
async function deleteArticle() {
const response = await fetch(`${backend_base_url}/article/${obj_id}/`, {
method: 'DELETE',
headers: {
"Authorization": "Bearer " + localStorage.getItem("access"),
"access-control-allow-origin": "*"
},
}
)
if (response.status == 200) {
window.location.replace(`${frontend_base_url}/index.html`);
response_json = await response.json()
return response_json
} else {
alert(response.status)
}
article_detail.js
/// 게시물 수정
async function getUpdateData() {
let contents = document.getElementById("contents-update").value
console.log(contents)
updateArticle(contents)
}
// 게시물 삭제
async function removeArticle() {
await deleteArticle(url_id)
window.location.replace(`${fronted_base_url}/templates/article.html`)
}
artice/views.py
try:
board = BoardModel.objects.get(name=request.data.get('board'))
article = ArticleModel.objects.create(
article_author = request.user,
article_title = request.data.get('article_title',''),
article_contents = request.data.get('article_contents',''),
article_image = request.FILES['article_image'],
article_exposure_date = request.data.get('article_exposure_date',''),
board = board,
)
except:
board = BoardModel.objects.get(name=request.data.get('board'))
article = ArticleModel.objects.create(
article_author = request.user,
article_title = request.data.get('article_title',''),
article_contents = request.data.get('article_contents',''),
article_exposure_date = request.data.get('article_exposure_date',''),
board = board,
)