投票機能を実装したいと思います。投票機能はオブジェクトを取得できません。 vote.jsは大丈夫です。何か案が? POSTリクエストは送信されないようです。ありがとうございました。vote-function "Eintragは指定されたクエリと一致しません"
これは誤りである:result.htmlで
Page not found (404)
Request Method: GET
Request URL: http://.../vote/
Raised by: book.views.vote
No Eintrag matches the given query.
スニペット:
<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()
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年午後06時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