Почему я получаю ошибку 500 при получении javascript? [закрыто]
У меня есть проект Django, и я хочу обновить модель, используя javascript fetch method put для обновления значения true => false и наоборот. однако я получаю ошибку 500 inbox.js:63 PUT http://127.0.0.1:8000/add-like/14 500 (Internal Server Error)
.
и я не могу понять почему.
Ps. Я начинающий
views.py :
@csrf_exempt
@login_required
def Like_post(request, id):
if request.method == 'PUT':
user = User.objects.get(username=request.user)
post_id = post.objects.get(pk=id)
if post_likes.objects.filter(like_user = user , liked_post=post_id):
remove = post_likes.objects.get(like_user=user, liked_post=post_id).delete()
remove.save()
else :
add = post_likes.objects.create(like_user=user, liked_post=post_id)
add.save()
all_likes_on_a_post = post_likes.liked_post.all().count()
return JsonResponse({'like_count': all_likes_on_a_post, "message": "successfull.", "status": 201})
else:
like_post = post.objects.filter(pk=id)
array = [one.serialize() for one in like_post]
return JsonResponse(array, safe=False)
js файл:
fetch(`load_posts`)
.then(response => response.json())
.then(page_obj => {
// Print emails
console.log(page_obj);
page_obj.forEach(Post => show_post(Post));
});
function show_post(Post){
let postsWrapper = document.createElement('div');
postsWrapper.className = "post-wrapper";
let postHeader = document.createElement('div');
postHeader.className = "post-header"
let creator = document.createElement('p')
creator.innerHTML = `@${Post.creator}`
creator.style.display = "inline"
let pubdate = document.createElement('p')
pubdate.innerHTML = Post.pub_date
pubdate.style.display = "inline"
pubdate.style.marginLeft = "40px"
postHeader.append(creator)
postHeader.append(pubdate)
postsWrapper.append(postHeader)
let postContent = document.createElement('div');
postContent.className = "post-content"
postContent.innerHTML = Post.content
postContent.style.marginTop = "20px"
postsWrapper.append(postContent)
let postFooter = document.createElement('div');
let likeButton = document.createElement('i')
if (Post.is_liked == true) {
likeButton.className = "fas fa-heart"
likeButton.style.color = "red"
} else{
likeButton.className = "far fa-heart"
likeButton.style.color = "#fff"
}
likeButton.style.display = "inline"
postFooter.append(likeButton)
likeButton.addEventListener('click', () => add_like(Post, likeButton))
postsWrapper.append(postFooter)
let htmlDiv = document.querySelector('.posts-container')
htmlDiv.append(postsWrapper)
}
function add_like(Post, likeButton){
const like_value = !Post.is_liked
console.log(`updating ${Post.id} Like as ${like_value} `);
fetch(`/add-like/${Post.id}`, {
method: 'PUT',
body: JSON.stringify({
is_liked: like_value
})
})
console.log(Post)
if (like_value == true){
likeButton.className = "fas fa-heart"
likeButton.style.color = "red"
} else {
likeButton.className = "far fa-heart"
likeButton.style.color = "#fff"
}
}
});
URL работает отлично, но единственная проблема заключается в методе put.