私の Django ビューでは、コンテキスト変数に変数を代入しようとしても機能しません。基本的に、リクエストからデータを取得し、リクエストに基づいて検索を実行し、その検索結果をコンテキストに割り当てます。これが私のview.pyです: EDIT2
def home(request):
GET = request.GET
utilisateur = ""
context = {}
if GET.has_key('name'):
me = UserProfile.objects.filter(facebookId=GET[u'name'])
utilisateur = me[0].facebookId
print utilisateur
context.update({"nom":str(utilisateur)})
print context.get("nom")
return render_to_response('home.html',context)
else:
print "Request doenst have a Name variable"
return render_to_response("home.html",context,
context_instance=RequestContext(request))
変数を数値に置き換えてもvar
、コンテキストを介してテンプレートに送信することはできませんが、コンソールからその値を確認できます(print var
行に注意してください)
私はここで何か悪いことをしていますか? *編集*
私のhome.htmlファイル:
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
</head>
<body>
<div id="fb-root"></div>
<script>
var username = "";
$(document).ready(function(){
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
// Init the SDK upon load
window.fbAsyncInit = function() {
FB.init({
appId : '302210766529054', // App ID
channelUrl : '//'+window.location.hostname+'/channel', // Path to your Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response) {
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
FB.api('/me', function(me){
if (me.username) {
document.getElementById('auth-displayname').innerHTML = me.name;
username = me.username;
document.getElementById('picture').innerHTML = "<img src = 'https://graph.facebook.com/"+me.username+"/picture'>";
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET','?name='+me.username, true);
xmlhttp.send();
}
})
document.getElementById('auth-loggedout').style.display = 'none';
document.getElementById('auth-loggedin').style.display = 'block';
} else {
// user has not auth'd your app, or is not logged into Facebook
document.getElementById('auth-loggedout').style.display = 'block';
document.getElementById('auth-loggedin').style.display = 'none';
}
});
// respond to clicks on the login and logout links
document.getElementById('auth-loginlink').addEventListener('click', function(){
FB.login();
});
document.getElementById('auth-logoutlink').addEventListener('click', function(){
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET','?name='+username+'&logout=True', true);
xmlhttp.send();
FB.logout();
});
}
}
);
</script>
<h1>Skèmpi:Rediscover your heritage</h1>
<div id="auth-status">
<div id="auth-loggedout">
<a href="#" id="auth-loginlink">Login</a>
</div>
<div id="auth-loggedin" style="display:none">
<div id="picture"></div>
Hi, <span id="auth-displayname"></span>
(<a href="#" id="auth-logoutlink">logout</a>)
{{ nom }}
</div>
</div>
編集3
Quit the server with CONTROL-C.
[21/May/2012 01:10:35] "GET /skempi/home HTTP/1.1" 301 0
Request doesn't have a Name variable
[21/May/2012 01:10:35] "GET /skempi/home/ HTTP/1.1" 200 3179
dontshare
[21/May/2012 01:10:35] "GET /skempi/home/?name=dontshare HTTP/1.1" 200 3188