我在django项目中遇到了ajax请求的问题。我试图通过在任何地方添加打印语句来调试视图代码,直到我发现它似乎停止并返回500错误。我不知道为什么会这样,所以我希望有经验的人会知道出了什么问题。
我正在使用的javascript库是jQuery(仅用于ajax调用)和GogoMakePlay(GogoMakePlay.com)
该应用程序是一个基于浏览器的国际象棋游戏,后端有django和javascript(GogoMakePlay),用于显示国际象棋棋盘。在这种情况下,会显示棋盘,当我点击一块时,它应该对django视图进行ajax调用,该视图返回可能移动的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调用。
我一直在谷歌搜索这几个小时,只发现上述链接与我的问题有关。通常StackOverflow有所有答案,但在这种情况下,我被迫创建一个帐户,以便我可以提出这个问题。如果有一个类似的问题解决我的问题,请原谅我。
所以我的问题是:
提前谢谢=)
答案 0 :(得分:2)
因此,错误实际上在您的调试代码中:
print "created the piece: " + piece
piece
是Pawn的一个实例,正如错误所说,你不能只是用随机对象连接字符串。 Python不是PHP - 它是强类型的,为了做到这一点,你需要将实例转换为它的字符串表示。最好的方法是使用字符串格式
print "created the piece: %s" % piece
将调用对象的__unicode__
方法将其转换为字符串。
请注意,您应该使用日志记录调用而不是打印 - 除了其他任何内容,如果您在部署时不小心在代码中留下了print语句,一切都会中断。所以它应该是:
logging.info('created the piece: %s', piece)
(日志记录调用接受参数并自行进行字符串插值,因此您不需要像打印那样使用%
运算符。)