error to save history chess into db sqlite django
up vote
0
down vote
favorite
I use django 2.1 and combine its with nodejs.
the game is ok but when game end, I want to save history into db of django. But it's not run. I'm not sure where is issue
staticmain.js
winner = checkLogout !== username ? username : checkLogout;
history= game.history();
socket.emit('endgame', {'gameId':gameId,'player1':player1, 'player2':player2, 'winner': winner, 'history': history.toString(), note : checkLogout });
nodejsindex.js
var http = require('http').createServer().listen(4000);
var io = require('socket.io')(http);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhttp = new XMLHttpRequest();
var host = 'localhost';
var port = '8000';
io.on('connection', function (socket) {
socket.on('endgame', function(msg){
var url = 'http://' + host +':' + port + '/save_game/';
xhttp.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
if(xhttp.responseText === "error")
console.log("error saving game");
else if(xhttp.responseText === "success")
console.log("the message was posted successfully");
}
};
xhttp.open('POST', url, true);
xhttp.send(JSON.stringify(msg));
});
});
gameviews.py
import json
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from game.models import Game
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
@login_required
def index(request):
return render(request, 'index.html')
@csrf_exempt
def save_game(request):
if request.method == 'POST':
msg_obj = json.loads(request.body.decode('utf-8'))
try:
msg = Game.objects.create(gameId=msg_obj['gameId'], player1=msg_obj['player1'], player2=msg_obj['player2'], winner = msg_obj['winner'], history = msg_obj['history'])
msg.save()
except:
print("error saving game")
return HttpResponse("error")
return HttpResponse("success")
else:
return HttpResponseRedirect('/')
update code staticmain.js
socket.on('joingame', function (msg) {
gameId = msg.game.gameId;
player1 = username;
player2 = oppDict;});
python node.js django
|
show 1 more comment
up vote
0
down vote
favorite
I use django 2.1 and combine its with nodejs.
the game is ok but when game end, I want to save history into db of django. But it's not run. I'm not sure where is issue
staticmain.js
winner = checkLogout !== username ? username : checkLogout;
history= game.history();
socket.emit('endgame', {'gameId':gameId,'player1':player1, 'player2':player2, 'winner': winner, 'history': history.toString(), note : checkLogout });
nodejsindex.js
var http = require('http').createServer().listen(4000);
var io = require('socket.io')(http);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhttp = new XMLHttpRequest();
var host = 'localhost';
var port = '8000';
io.on('connection', function (socket) {
socket.on('endgame', function(msg){
var url = 'http://' + host +':' + port + '/save_game/';
xhttp.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
if(xhttp.responseText === "error")
console.log("error saving game");
else if(xhttp.responseText === "success")
console.log("the message was posted successfully");
}
};
xhttp.open('POST', url, true);
xhttp.send(JSON.stringify(msg));
});
});
gameviews.py
import json
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from game.models import Game
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
@login_required
def index(request):
return render(request, 'index.html')
@csrf_exempt
def save_game(request):
if request.method == 'POST':
msg_obj = json.loads(request.body.decode('utf-8'))
try:
msg = Game.objects.create(gameId=msg_obj['gameId'], player1=msg_obj['player1'], player2=msg_obj['player2'], winner = msg_obj['winner'], history = msg_obj['history'])
msg.save()
except:
print("error saving game")
return HttpResponse("error")
return HttpResponse("success")
else:
return HttpResponseRedirect('/')
update code staticmain.js
socket.on('joingame', function (msg) {
gameId = msg.game.gameId;
player1 = username;
player2 = oppDict;});
python node.js django
Yourexcept
block is throwing away the error message, which probably contains useful information. Try this instead:except Exception as ex:
, and thenprint("error saving game: %s" % ex)
– John Gordon
Nov 21 at 22:36
@JohnGordon, that notice:[21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7
but nodejs notice successthe message was posted successfully
– Tran Quoc Gia Cat
Nov 21 at 22:43
That error message seems to say thatmsg_obj
does not contain agameId
. Try printingmsg_obj
before theGame.object.create()
call.
– John Gordon
Nov 21 at 22:50
@JohnGordon, gameId not here{'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId'
and history not to convert to string
– Tran Quoc Gia Cat
Nov 21 at 22:57
Okay then, the error is obvious -- your caller is not supplying agameId
argument. How you handle that error is up to you.
– John Gordon
Nov 21 at 23:19
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I use django 2.1 and combine its with nodejs.
the game is ok but when game end, I want to save history into db of django. But it's not run. I'm not sure where is issue
staticmain.js
winner = checkLogout !== username ? username : checkLogout;
history= game.history();
socket.emit('endgame', {'gameId':gameId,'player1':player1, 'player2':player2, 'winner': winner, 'history': history.toString(), note : checkLogout });
nodejsindex.js
var http = require('http').createServer().listen(4000);
var io = require('socket.io')(http);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhttp = new XMLHttpRequest();
var host = 'localhost';
var port = '8000';
io.on('connection', function (socket) {
socket.on('endgame', function(msg){
var url = 'http://' + host +':' + port + '/save_game/';
xhttp.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
if(xhttp.responseText === "error")
console.log("error saving game");
else if(xhttp.responseText === "success")
console.log("the message was posted successfully");
}
};
xhttp.open('POST', url, true);
xhttp.send(JSON.stringify(msg));
});
});
gameviews.py
import json
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from game.models import Game
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
@login_required
def index(request):
return render(request, 'index.html')
@csrf_exempt
def save_game(request):
if request.method == 'POST':
msg_obj = json.loads(request.body.decode('utf-8'))
try:
msg = Game.objects.create(gameId=msg_obj['gameId'], player1=msg_obj['player1'], player2=msg_obj['player2'], winner = msg_obj['winner'], history = msg_obj['history'])
msg.save()
except:
print("error saving game")
return HttpResponse("error")
return HttpResponse("success")
else:
return HttpResponseRedirect('/')
update code staticmain.js
socket.on('joingame', function (msg) {
gameId = msg.game.gameId;
player1 = username;
player2 = oppDict;});
python node.js django
I use django 2.1 and combine its with nodejs.
the game is ok but when game end, I want to save history into db of django. But it's not run. I'm not sure where is issue
staticmain.js
winner = checkLogout !== username ? username : checkLogout;
history= game.history();
socket.emit('endgame', {'gameId':gameId,'player1':player1, 'player2':player2, 'winner': winner, 'history': history.toString(), note : checkLogout });
nodejsindex.js
var http = require('http').createServer().listen(4000);
var io = require('socket.io')(http);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhttp = new XMLHttpRequest();
var host = 'localhost';
var port = '8000';
io.on('connection', function (socket) {
socket.on('endgame', function(msg){
var url = 'http://' + host +':' + port + '/save_game/';
xhttp.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
if(xhttp.responseText === "error")
console.log("error saving game");
else if(xhttp.responseText === "success")
console.log("the message was posted successfully");
}
};
xhttp.open('POST', url, true);
xhttp.send(JSON.stringify(msg));
});
});
gameviews.py
import json
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from game.models import Game
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
@login_required
def index(request):
return render(request, 'index.html')
@csrf_exempt
def save_game(request):
if request.method == 'POST':
msg_obj = json.loads(request.body.decode('utf-8'))
try:
msg = Game.objects.create(gameId=msg_obj['gameId'], player1=msg_obj['player1'], player2=msg_obj['player2'], winner = msg_obj['winner'], history = msg_obj['history'])
msg.save()
except:
print("error saving game")
return HttpResponse("error")
return HttpResponse("success")
else:
return HttpResponseRedirect('/')
update code staticmain.js
socket.on('joingame', function (msg) {
gameId = msg.game.gameId;
player1 = username;
player2 = oppDict;});
python node.js django
python node.js django
edited Nov 21 at 23:00
asked Nov 21 at 22:32
Tran Quoc Gia Cat
406
406
Yourexcept
block is throwing away the error message, which probably contains useful information. Try this instead:except Exception as ex:
, and thenprint("error saving game: %s" % ex)
– John Gordon
Nov 21 at 22:36
@JohnGordon, that notice:[21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7
but nodejs notice successthe message was posted successfully
– Tran Quoc Gia Cat
Nov 21 at 22:43
That error message seems to say thatmsg_obj
does not contain agameId
. Try printingmsg_obj
before theGame.object.create()
call.
– John Gordon
Nov 21 at 22:50
@JohnGordon, gameId not here{'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId'
and history not to convert to string
– Tran Quoc Gia Cat
Nov 21 at 22:57
Okay then, the error is obvious -- your caller is not supplying agameId
argument. How you handle that error is up to you.
– John Gordon
Nov 21 at 23:19
|
show 1 more comment
Yourexcept
block is throwing away the error message, which probably contains useful information. Try this instead:except Exception as ex:
, and thenprint("error saving game: %s" % ex)
– John Gordon
Nov 21 at 22:36
@JohnGordon, that notice:[21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7
but nodejs notice successthe message was posted successfully
– Tran Quoc Gia Cat
Nov 21 at 22:43
That error message seems to say thatmsg_obj
does not contain agameId
. Try printingmsg_obj
before theGame.object.create()
call.
– John Gordon
Nov 21 at 22:50
@JohnGordon, gameId not here{'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId'
and history not to convert to string
– Tran Quoc Gia Cat
Nov 21 at 22:57
Okay then, the error is obvious -- your caller is not supplying agameId
argument. How you handle that error is up to you.
– John Gordon
Nov 21 at 23:19
Your
except
block is throwing away the error message, which probably contains useful information. Try this instead: except Exception as ex:
, and then print("error saving game: %s" % ex)
– John Gordon
Nov 21 at 22:36
Your
except
block is throwing away the error message, which probably contains useful information. Try this instead: except Exception as ex:
, and then print("error saving game: %s" % ex)
– John Gordon
Nov 21 at 22:36
@JohnGordon, that notice:
[21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7
but nodejs notice success the message was posted successfully
– Tran Quoc Gia Cat
Nov 21 at 22:43
@JohnGordon, that notice:
[21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7
but nodejs notice success the message was posted successfully
– Tran Quoc Gia Cat
Nov 21 at 22:43
That error message seems to say that
msg_obj
does not contain a gameId
. Try printing msg_obj
before the Game.object.create()
call.– John Gordon
Nov 21 at 22:50
That error message seems to say that
msg_obj
does not contain a gameId
. Try printing msg_obj
before the Game.object.create()
call.– John Gordon
Nov 21 at 22:50
@JohnGordon, gameId not here
{'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId'
and history not to convert to string– Tran Quoc Gia Cat
Nov 21 at 22:57
@JohnGordon, gameId not here
{'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId'
and history not to convert to string– Tran Quoc Gia Cat
Nov 21 at 22:57
Okay then, the error is obvious -- your caller is not supplying a
gameId
argument. How you handle that error is up to you.– John Gordon
Nov 21 at 23:19
Okay then, the error is obvious -- your caller is not supplying a
gameId
argument. How you handle that error is up to you.– John Gordon
Nov 21 at 23:19
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421369%2ferror-to-save-history-chess-into-db-sqlite-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your
except
block is throwing away the error message, which probably contains useful information. Try this instead:except Exception as ex:
, and thenprint("error saving game: %s" % ex)
– John Gordon
Nov 21 at 22:36
@JohnGordon, that notice:
[21/Nov/2018 23:40:59] "GET / HTTP/1.1" 200 1127 [21/Nov/2018 23:41:07] "GET /accounts/login/?next=/ HTTP/1.1" 200 446 [21/Nov/2018 23:41:10] "POST /accounts/login/?next=/ HTTP/1.1" 302 0 [21/Nov/2018 23:41:10] "GET / HTTP/1.1" 200 1127 error saving game: 'gameId' [21/Nov/2018 23:41:15] "POST /save_game/ HTTP/1.1" 200 7
but nodejs notice successthe message was posted successfully
– Tran Quoc Gia Cat
Nov 21 at 22:43
That error message seems to say that
msg_obj
does not contain agameId
. Try printingmsg_obj
before theGame.object.create()
call.– John Gordon
Nov 21 at 22:50
@JohnGordon, gameId not here
{'player1': 'han', 'player2': 'cat', 'winner': 'han', 'history': '[object History]', 'note': 'cat'} error saving game: 'gameId'
and history not to convert to string– Tran Quoc Gia Cat
Nov 21 at 22:57
Okay then, the error is obvious -- your caller is not supplying a
gameId
argument. How you handle that error is up to you.– John Gordon
Nov 21 at 23:19