0

在我的 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
4

1 回答 1

0

因此,似乎这里可能发生了很多事情。

您是否验证settings.TEMPLATE_DIRS设置正确并且home.html模板实际上正在呈现?为了验证这一点,当您尝试在 Web 浏览器中加载页面时,是否会出现 SDK 样板文件?

我假设你点击了正确的 URL - 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))

然后呈现的页面应显示返回的 Facebook ID,无论您将{{ nom }}.

于 2012-05-21T04:16:36.997 回答