我創建一個Django的web應用程序其特點類似,這是什麼網站上投票系統工作。無法獲取的jQuery的Ajax提交表單和Django
目前,當你點擊一個給予好評或downvote箭,我使用jQuery來處理顏色的變化,並在投票評分的變化(投票數)。我基本上將原來從數據庫中提取的投票分數相應地加上或減去一個,然後顯示這個數字。我沒有更新數據庫中的分數,因此投票似乎與用戶一致(如果其他人在用戶在網站上投了票)。
我有阿賈克斯部分的麻煩。基本上,當用戶投票,我還是想送給予好評或downvote提交到服務器並更新數據庫,但沒有刷新頁面。當我不添加「返回false」到ajax調用結束時,我的數據庫被更新,但是我的頁面刷新了。但是,當我將「返回false」添加到ajax調用的結尾時,我的頁面不會刷新,但數據庫也不會更新。
這裏是我的形式:
<form method="post" class="voting-button" action="/sentence/vote/{{sentence.id}}/">
{% csrf_token %}
<input type="submit" class="upvote_on" name="upvote" value="" />
<p class="vote-score">{{sentence.total_votes}}</p>
<input type="submit" class="downvote_off" name="downvote" value="" />
</form>
它與兩個提交按鈕的一種形式:一個是給予好評,一個用於downvote。
這是因爲當你點擊給予好評按鈕的javascript:
<script type="text/javascript">
//script to control arrows and the number of votes shown
$("[name='upvote']").click(function(){
if ($(this).attr("class") == "upvote_off") {
$(this).attr("class", "upvote_on");
//If upvote is off and downvote is off
if ($(this).siblings("[name='downvote']").attr("class") == "downvote_off"){
var score = $(this).siblings(".vote-score").text();
scoreInt = parseInt(score)
scoreInt += 1;
$(this).siblings(".vote-score").text(scoreInt);
} else { //if upvote is off and downvote is on
var score = $(this).siblings(".vote-score").text();
scoreInt = parseInt(score)
scoreInt += 2;
$(this).siblings(".vote-score").text(scoreInt);
}
$(this).siblings("[name='downvote']").attr("class", "downvote_off");
} else {
$(this).attr("class", "upvote_off");
$(this).siblings("[name='downvote']").attr("class", "downvote_off");
var score = $(this).siblings(".vote-score").text();
scoreInt = parseInt(score)
scoreInt -= 1;
$(this).siblings(".vote-score").text(scoreInt);
}
$.post(
'/sentence/vote/{{sentence.id}}/',
{
name: "upvote",
},
function(response){
$("#divText").text("hello world!");
}
)
})
基本上,它的主要內容是,給予好評按鈕可以有兩種類別之一:「uvpote_off」或「upvote_on」取決於用戶是否上傳或不提供。 if else語句只是爲了使這些狀態改變正確。
這裏是我的Django的查看功能,其形式發送至:
def vote(request, sentence_id):
p = get_object_or_404(Sentence, pk=sentence_id)
if 'upvote' in request.POST:
try:
v = Vote.objects.filter(voter = request.user).get(sentence=p)
if v.score == 0:
v.score = 1
elif v.score == 1:
v.score = 0
else: #for case where v.score = -1
v.score = 1
v.save()
except Vote.DoesNotExist:
v = Vote(voter =request.user, sentence=p, score=1)
v.save()
elif 'downvote' in request.POST:
try:
v = Vote.objects.filter(voter = request.user).get(sentence=p)
if v.score == 0:
v.score = -1
elif v.score == -1:
v.score = 0
else: #for case where v.score = 1
v.score = -1
v.save()
except Vote.DoesNotExist:
v = Vote(voter = request.user, sentence=p, score=1)
v.save()
return HttpResponseRedirect(reverse('sentence.views.show_sentence_order', args=(p.sentence_order,)))
到底是什麼問題就在這裏?過去幾個小時我一直在爲此苦苦掙扎,看了一大堆教程,但無法弄清楚我做錯了什麼。
非常感謝您的幫助。
嘿詹姆斯,謝謝你的效率指針。這實際上是我第一次編寫一個Django應用程序,所以仍然沒有一個非常有效的代碼設計。該教程非常有趣,它回答了我在django中設置ajax的許多問題。主要問題實際上是將csrf_token作爲POST數據傳遞:https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax。我會把它作爲答案進一步詳細說明。 – WarAndPiece 2012-02-24 23:06:55
@WarAndPiece謝謝你,如果我的帖子很有幫助,請接受答案並投票,如果不是,請保持原狀;) – 2012-02-24 23:09:15