투표 기능을 구현하고 싶습니다. vote 함수는 객체를 가져올 수 없습니다. vote.js는 괜찮습니다. 어떤 생각? POST 요청이 전송되지 않는 것 같습니다. 고맙습니다.vote-function "Eintrag가 주어진 쿼리와 일치하지 않습니다."
<a href="/vote/" id="eintrag-vote-{{ eintrag.id }}" class="vote">▲</a>
<p id="eintrag-title-{{ eintrag.id }}">{{ eintrag.title }}</p>
models.py :
class Eintrag(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=200)
points = models.IntegerField(default=1)
text = models.TextField()
views.py :
@login_required
def vote(request):
eintrag = get_object_or_404(Eintrag, id=request.POST.get('eintrag'))
eintrag.points += 1
eintrag.save()
return HttpResponse()
result.html에서 Page not found (404)
Request Method: GET
Request URL: http://.../vote/
Raised by: book.views.vote
No Eintrag matches the given query.
조각 :
이 오류입니다
urls.py :
url(r'^vote/$', views.vote, name='vote'),
및 vote.js는 :
$(document).ready(function() {
// using jQuery
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
function.vote(eintragID) {
$.ajax({
type: "POST",
url: "/vote/",
data: {
"eintrag": eintragID
},
success: function() {
$("#eintrag-vote-" + eintragID).hide();
$("#eintrag-title-" + eintragID).css({
"margin-left": "15px"
});
},
headers: {
'X-CSRFToken': csrftoken
}
});
return false;
}
$("a.vote").click(function() {
var eintragID = parseInt(this.id.split("-")[2]);
return vote(eintragID);
})
});
같은 오류 : 퍼티 말한다 : 찾을 수 없음 :/투표/ [12/6월/2016 18시 17분 15초] "GET/투표/HTTP/1.1"404 1722 – royaIT
글쎄, 당신은 항상거야 GET 요청을하는 경우 문제가 발생하면 POST 요청으로 변경해야합니다. JS 파일을 변경 한 후에 collectstatic을 잊지 마라. – raphv
'code' function.vote (eintragID) { $ 아약스 ({ 방법 : "POST", URL : "/ 투표 /", 데이터 : { "eintrag"eintragID}, 성공 : 기능() $ ("#eintrag-vote-"+ eintragID) .hide(); $ ("# eintrag-title-"+ eintragID) .css ({ "margin-left": "15px"}) , 헤더 : { 'X-CSRFToken': csrftoken } }); false를 반환합니다. }'code' – royaIT