在我的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
答案 0 :(得分:0)
所以,似乎这里可能会有很多事情发生。
您是否确认settings.TEMPLATE_DIRS
设置正确并且home.html
模板实际上正在呈现?要验证这一点,当您尝试在Web浏览器中加载页面时,是否会显示SDK样板?
我假设你正在点击正确的网址 - http://127.0.0.1:8000/?name=Mohamed
。这是对的吗?
最后,print
语句是否会返回任何内容 - 也就是说,您是否从UserProfile
查询中获得响应?
在验证了所有这些内容之后,您正在使用的上下文变量名称之间存在一些混淆。在views.py
中,您设置了上下文变量temp
,但在模板home.html
中,您尝试输出上下文变量nom
。这就是我实现视图的方式:
def home(request):
context = {}
if 'name' in request.GET:
me = UserProfile.objects.filter(facebookId=request.GET[u'name'])
context['nom'] = str(me[0].facebookId)
print context['nom'] # if you really want see what's been stored
else:
print "Request doesn't have a Name variable"
return render_to_response("home.html", context,
context_instance=RequestContext(request))
然后,无论您将{{ nom }}
置于何处,呈现的页面都应显示返回的Facebook ID。