JSONDecodeError при использовании json.loads
Я получаю Expecting value: line 1 column 1 (char 0)
при попытке использовать json.loads
Просматривал похожие вопросы, но не смог найти решение для своего кода.
Выдает ошибку для строки: body = json.loads(body_unicode)
network.js:
//When user clicks the save button
save_button.addEventListener("click", event => {
new_content = document.getElementByID(`new_content_${postID}`);
//Make fetch request to update the page
fetch(`/edit/${postID}`,{
method: "POST",
body: JSON.stringify({
content: new_content.value,
})
})
.then(response => {
if(response.ok || response.status == 400) {
return response.json()
} else {
return.Promise.reject("Error:" + response.status)
}
})
.then(result => {
new_content.innerHTML = result.content;
cancelEdit(postID);
})
.catch(error => {
console.error(error);
})
})
views.py:
def edit(request, id):
if request.method == "POST":
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
new_content = body['content']
AllPost.objects.filter(id = id).update(content = new_content)
return JsonResponse({"message": "Post updated successfully.", "content": new_content}, status=200)
return JsonResponse({"error": "Bad Request"}, status = 400)
index.html:
{% for post in page_obj %}
{{ post.full_name|upper }}<br>
<div class="frame">
<h4><a href="{% url 'profile' post.user.username %}" style="color: black;">{{post.user.username}}</a></h4>
{% if post.user == user %}
<button class="btn btn-sm btn-outline-primary" data-id="{{post.id}}" id="edit_button_{{post.id}}">Edit</button>
{% endif %}
<div id="content_{{post.id}}">{{post.content}}</div>
<form action="{% url 'edit' post.id %}" method="post" id="edit_form_{{post.id}}" style="display: none;">
{% csrf_token %}
<div class="form-group"><textarea id="new_content_{{post.id}}" name="new_content" cols="30"></textarea></div>
<input class="btn btn-sm btn-success" id="save_{{post.id}}" type="submit" value="Save">
<input class="btn btn-sm btn-danger" id="cancel_{{post.id}}" type="submit" value="Cancel">
</form>
<div class="grey" id="timestamp">{{post.date}}</div>
<div class="grey">{{post.likes}}</div>
<a href="#" style="color: grey;">Comment</a>
</div>