在我的Django視圖中,當我嘗試將變量分配給我的上下文變量時,它不起作用。我基本上從請求中獲取數據,根據請求執行搜索,並將該搜索結果分配給我的上下文。這裏是我的view.py: EDIT2無法發送上下文變量到Django中的模板
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
向我們顯示''home.html'' – seler
另外,''context''字典沒有被髮送到模板,只是''{「username」:var}''。 – seler
我現在編輯過,仍然是同樣的事情。在我的home.html中,我只是在某處(其他基本上是JavaScript SDK初始化等),我應該發佈它嗎? –