Нравится и не нравится django ajax
пишу приложение для like и like используя Django и ajax, JQuery функции, но я заметил, что оно работает нормально, но проблема в том, что если нажать кнопку like, то я получаю 500 внутренних ошибок. Вот POST http://127.0.0.1:8000/like/ 500 (Внутренняя ошибка сервера)##
здесь код ajax при выполнении я получаю 500 внутреннюю ошибку.
$('#result').click(function() {
var liked = $(".like").val();
var addLike = liked + 1
data = {
'liked': addLike
}
console.log('data', data)
SendAjax();
});
// const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
function SendAjax() {
$.ajax({
url: '/like/',
method: 'POST',
headers: {
'X-CSRFToken': csrftoken
},
dataType: "json",
data: data,
success: function(data) {
console.log('success', data.status);
if (data.status === 200) {
console.log(data.status)
// let load_url = 'http://127.0.0.1:8000/' + name;
// console.log('load url', load_url);
// window.location.replace(load_url);
}
},
})
};
views.py
def like(request) : data= {}
name=get_object_or_404(Stylish, id=request.POST.get('id')) context= {
'name': name
}
print('context', context) print("like") if request.method=='POST' and "GET" : liked=request.POST('liked', None) print('liked', liked) like=likes.objects.create( liked=liked) print('like', like) return render(request, 'index.html', data)
<div class="col">
<input type="text" class="form-control text-1 textfont copybutton " data-clipboard-action="copy" data-clipboard-target="#copy_0" for="textfont" style="text-align: center; " value="𝔓𝔯𝔢𝔳𝔦𝔢𝔴 𝔗𝔢𝔵𝔱" id="copy_0" readonly="readonly">
<!-- {% if instance.liked%} -->
<span id="count"></span>
<input class="like" type="image" src="{% static 'images/like.svg' %}" alt="Like" id="like" title="Like" width="10" height="10" id="heart" value="{{ liked }}">
<!-- {% else %} -->
<input class="like" type="image" src="{% static 'images/like.svg' %}" alt="Like" id="like" title="Like" width="10" height="10" id="heart">
<!-- {% endif %} -->
<input class="unlike" type="image" src="{% static 'images/dislike.png' %}" alt="unlike" id="unlike" title="unlike" width="10" height="10" id="heart">
</div>
<div class="col">
<input type="text" class="form-control text-2 textfont copybutton" style="cursor:pointer;" data-clipboard-action="copy" data-clipboard-target="#copy_1" for="textfont" style="text-align: center; " value="𝕻𝖗𝖊𝖛𝖎𝖊𝖜 𝕿𝖊𝖝𝖙" id="copy_1" readonly="readonly">
<input class="like" type="image" src="{% static 'images/like.svg' %}" alt="Like" id="like" title="Like" width="10" height="10" id="heart">
<input class="unlike" type="image" src="{% static 'images/dislike.png' %}" alt="unlike" id="unlike" title="unlike" width="10" height="10" id="heart">
<span id="count"></span>
</div>
<div class="col">
<input type="text" class="form-control text-3 textfont copybutton" style="cursor:pointer;" data-clipboard-action="copy" data-clipboard-target="#copy_2" for="textfont" style=" text-align: center;" value="✋♪ Ƥ𝔯Ⓔᐯ𝔦乇ω т乇𝐗𝐭 😈🌷" id="copy_2" readonly="readonly">
<input type="image" src="{% static 'images/like.svg' %}" alt="Like" id="like" title="Like" width="10" height="10" id="heart">
<input class="unlike" type="image" src="{% static 'images/dislike.png' %}" alt="unlike" id="unlike" title="unlike" width="10" height="10" id="heart">
<span id="count"></span>
</div>
def like(request):
data = {}
name = get_object_or_404(Stylish, id=request.POST.get('id'))
context = {'name': name}
print('context', context)
print("like")
if request.method == 'POST' and "GET":
liked=request.POST('liked',None)
print('liked', liked)
like=likes.objects.create( liked=liked)
print('like', like)
return render(request, 'index.html', data)
<input class="unlike" type="image" src="{% static 'images/dislike.png' %}" alt="unlike" id="unlike" title="unlike" width= "10" height="10" id="heart" >
</div>
<div class="col">
<input type="text" class="form-control text-2 textfont copybutton" style="cursor:pointer;" data-clipboard-action="copy" data-clipboard-target="#copy_1" for="textfont" style="text-align: center; " value="𝕻𝖗𝖊𝖛𝖎𝖊𝖜 𝕿𝖊𝖝𝖙" id="copy_1" readonly="readonly">
<input class="like" type="image" src="{% static 'images/like.svg' %}" alt="Like" id="like" title="Like" width= "10" height="10" id="heart" >
<input class="unlike" type="image" src="{% static 'images/dislike.png' %}" alt="unlike" id="unlike" title="unlike" width= "10" height="10" id="heart" >
<span id="count"></span>
</div>
<div class="col">
<input type="text" class="form-control text-3 textfont copybutton" style="cursor:pointer;" data-clipboard-action="copy" data-clipboard-target="#copy_2" for="textfont" style=" text-align: center;" value="✋♪ Ƥ𝔯Ⓔᐯ𝔦乇ω т乇𝐗𝐭 😈🌷" id="copy_2" readonly="readonly">
<input type="image" src="{% static 'images/like.svg' %}" alt="Like" id="like" title="Like" width= "10" height="10" id="heart" >
<input class="unlike" type="image" src="{% static 'images/dislike.png' %}" alt="unlike" id="unlike" title="unlike" width= "10" height="10" id="heart">
<span id="count"></span>
</div>``