我在django項目中遇到了ajax請求的問題。我試圖通過在任何地方添加print語句來調試視圖代碼,直到我發現它似乎停止並返回500錯誤。我不知道爲什麼會發生這種情況,所以我希望有更多經驗的人知道發生了什麼問題。django無法在視圖中創建常規對象,返回500錯誤
我使用的JavaScript庫是jQuery的(僅適用於Ajax調用,雖然)和GogoMakePlay(GogoMakePlay.com)
應用程序是一個基於瀏覽器的國際象棋比賽在Django和JavaScript的後端(GogoMakePlay)用於顯示棋盤。在這種情況下,會顯示棋盤,當我點擊一塊棋子時,它應該對ajax調用django視圖,該視圖返回可能移動的json。我每次都點擊一個棋子,這樣我的打印功能就可以執行了,我應該能夠找到問題,但這次不行。
我的問題是幾乎相同的,因爲這一個:
Cannot create new Django model object within Ajax post request
除了與他不同,我的問題並沒有就此消失。
有問題的視圖:
def get_move_options(request):
if request.POST:
# initialise some variables based on the request type
pieceType = request.POST.get("pieceType")
pieceColour = request.POST.get("pieceColour")
pieceRow = request.POST.get("row")
pieceColumn = request.POST.get("column")
gameId = request.POST.get("gameId")
game = Game.objects.get(pk=gameId)
moves = Move.objects.filter(game=game)
print "initialised all the variables"
# check what type of piece it is
if pieceType == "pawn":
print "colour:" + pieceColour
piece = Pawn(pieceColour)
print "created the piece: " + piece # <-- this is never executed
elif pieceType == "king":
piece = King(pieceColour)
elif pieceType == "queen":
piece = Queen(pieceColour)
elif pieceType == "bishop":
piece = Bishop(pieceColour)
elif pieceType == "knight":
piece = Knight(pieceColour)
elif pieceType == "rook":
piece = Rook(pieceColour)
print "created the piece: " + piece
# make a new board and apply the moves to it
board = Board()
for move in moves:
board.makeMove(move)
print "made all the moves"
# get the possible moves
responseList = piece.getMoveOptions(pieceColumn, pieceRow, board)
return HttpResponse(json.dumps(responseList), mimetype="application/javascript")
的兵代碼:
class Pawn(Piece):
def __init__(self, colour):
print "creating object"
self.colour = colour
print "object created, the colour is: " + colour
*snip*
Ajax請求代碼:
*snip*
// get the csrf_token from the page TODO there must be a better way of doing this
var csrf_token = document.getElementById("csrf_token").getElementsByTagName("input")[0].value;
// add the variables to the post data
var data = {
"gameId" : gameId,
"pieceType" : piece.S.type,
"pieceColour" : piece.S.colour,
"column" : column,
"row" : row,
};
var url = "ajax/getMoveOptions";
// make the ajax call
$.ajax({
type : 'POST',
// add the csrf_token or we will get a 403 response
headers : {
"X-CSRFToken" : csrf_token
},
url : url,
data : data,
dataType : "json",
success : function(json) {
// loop through the json list
for(var i = 0; i < json.length; i++) {
// change the square colour of the options
var x = json[i]["row"];
var y = json[i]["column"];
var option = G.O["square" + y + x];
if(y % 2 == x % 2) {
var squareColour = "white";
} else {
var squareColour = "black";
}
option.setSrc("/static/images/board/" + squareColour + "Option.png").draw();
}
},
error : alert("I have now seen this too many times"),
});
*snip*
我的django控制檯輸出:
*snip*
[01/Jun/2012 02:07:45] "GET /static/images/chess_pieces/white/king.png HTTP/1.1" 200 1489
initialised all the variables
colour:white
creating object
object created, the colour is: white
[01/Jun/2012 02:07:48] "POST /game/ajax/getMoveOptions HTTP/1.1" 500 10331
我知道我可以在javascript中編寫代碼,但這是一個學校項目,我的目標之一是瞭解如何進行ajax調用。
我一直在Google上搜索幾個小時,發現只有前面提到的與我的問題有關的鏈接。通常StackOverflow有所有的答案,但在這種情況下,我被迫創建一個帳戶,以便我可以問這個問題。請原諒我是否有類似的問題解決我的問題。
所以我的問題是:
- 你需要更多的信息來幫助我嗎?如果是這樣,你需要知道什麼?
- 我的ajax調用是否正確?
- 如果是這樣,爲什麼會發出500錯誤?如果不是,那麼我做錯了什麼?
- 爲什麼視圖開始執行,但在Pawn對象創建後立即停止?
預先感謝您=)
您是否嘗試爲此行設置斷點:piece = Pawn(pieceColour)並檢查是否有任何異常,等等? – dbf
如果你得到一個500,那麼你會得到一個錯誤頁面發送到瀏覽器。你可以在Firebug/Chrome開發者工具中看到它。它說什麼? –
@丹尼羅斯曼謝謝你,我不知道我可以去找到「隱藏」500錯誤。無論如何它說:「不能連接'str'和'實例'對象」 – user1430067