为什么在下面的代码中,全局direktorie
是否在调试模板中返回正确的数据login()
,但是当我尝试从中访问相同的变量autoname()
时说列表的长度为0
?我不会direktorie
在任何其他地方views.py
——或其他任何地方提及此事。(下面的所有代码只是试图发现我做错了什么。我并不真正关心返回列表的长度。我只想知道它被看到并且具有大致正确数量的条目。 )
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404
import json # JSON for jQuery AJAX autocomplete
from eldappery import * # LDAP for jQuery AJAX autocomplete
direktorie = []
##############################################################################
def login(request):
"""LDAP Login routine"""
global direktorie
if request.method == "POST": # If submitted...
if request.POST["username"] and request.POST["password"]:
username = request.POST["username"]
password = request.POST["password"]
LDAPfeed = dapperize(username, password) # ...login
if LDAPfeed:
direktorie = fetch_names(LDAPfeed,"") # ...get everybody
ls = locals() # DEBUG!
gs = globals() # DEBUG!
return render_to_response("debug.html",
{"ls": ls,
"gs": gs}) # DEBUG! Works! (direktorie full)
else:
return HttpResponseRedirect("/login/")
return render_to_response("login.html",
context_instance=RequestContext(request))
##############################################################################
def autoname(request):
"""Auto-complete names"""
global direktorie
if request.is_ajax():
# results = [{"id": 5,
# "label": 5,
# "value": 5}] # DEBUG! Works! (5 in template)
results = [{"id": len(direktorie),
"label": len(direktorie),
"value": len(direktorie)}] # DEBUG! Doesn't work! (0 in template)
data = json.dumps(results) # Convert to JSON string
else: # No results returned!
data = "fail" # Error...
mimetype = "application/json" # MIME type = JSON
return HttpResponse(data, mimetype) # Send JSON back to web page
##############################################################################