LDAP search results "disappear"

44 views
Skip to first unread message

Kevin Cole

unread,
Dec 17, 2012, 1:03:59 PM12/17/12
to django...@googlegroups.com
Also asked on StackOverflow

http://stackoverflow.com/questions/13918785/django-ldap-search-results-disappear

Why in the code below, does the global direktorie return the correct data in the login() debug template, yet when I try to access the same variable from autoname() it says the list has a length of 0? I don't reference direktorie in any other places in views.py -- or anywhere else for that matter. (All of the code below is merely an attempt to discover what I'm doing wrong. I don't really care about the length of the returned list. I just want to know it's being seen and has roughly the right number of entries.)

##############################################################################

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 ##############################################################################

Bill Freeman

unread,
Dec 17, 2012, 1:42:04 PM12/17/12
to django...@googlegroups.com
Perhaps in the thread in which autoname is run, login has not yet been run.

Kevin Cole

unread,
Dec 17, 2012, 1:57:19 PM12/17/12
to django...@googlegroups.com
On Mon, Dec 17, 2012 at 1:42 PM, Bill Freeman <ke1...@gmail.com> wrote:

> Perhaps in the thread in which autoname is run, login has not yet been run.

And here I'll display my ignorance about threading. I've done nothing
specific to ask for threading. I wouldn't know how to thread even if I
had a sewing machine. ;-) I'm using apache prefork with mod-wsgi. In
testing, I go to the URL page first, which I had hoped would set the
global variable like everywhere else that I set values for small
handful of globals, and that going to the autoname URL after that
would see the global I had set.

Kevin Cole

unread,
Dec 17, 2012, 1:58:19 PM12/17/12
to django...@googlegroups.com
On Mon, Dec 17, 2012 at 1:57 PM, Kevin Cole <dc....@gmail.com> wrote:

> And here I'll display my ignorance about threading. I've done nothing
> specific to ask for threading. I wouldn't know how to thread even if I
> had a sewing machine. ;-) I'm using apache prefork with mod-wsgi. In
> testing, I go to the URL page first, which I had hoped would set the
> global variable like everywhere else that I set values for small
> handful of globals, and that going to the autoname URL after that
> would see the global I had set.

s/I go to the URL page first/I go to the login page first/

Bill Freeman

unread,
Dec 17, 2012, 2:53:09 PM12/17/12
to django...@googlegroups.com

Mind you, I'm not certain that it's a threading issue.  If this happens under runserver, it can't be threading because that's single threaded.  Most of the deployment things I've seen default to some multi (process, actually) threading, though.

Is it possible that fetch_names is returning something that times out somehow and then looks empty?  You might try rendering type(direktorie) in both views.  If it looks like a thing of some kind other than a list, then you might need to copy its parts in login, rather than saving it as is.

peter

unread,
Dec 17, 2012, 2:19:06 PM12/17/12
to django...@googlegroups.com
The desing of the view's of django, do not allow you to pass global
variables between the views. If you want to pass, data between views,
you need to use ajax, to send the data in a get or post requests.

def index(request):
print 'helou'
return render_to_response('index.html', {'lala': 'lala'})

#index.html
# <script type='text/javascript'>
# obj = {type:'get', url='/test/view0', data:
{tamangandapio:'yeah'}, type='json',
# response: function (){ console.log(response) }
# }
# $.ajax(obj);
# </script>

def view0(request)
#here you see the data send by the jquery code below
print request.GET
res = {'success':'i got it'}
return HttpResponse(simplejson.dumps(res),
mimetype='application/javascript')


please don't use global variables in views
Reply all
Reply to author
Forward
0 new messages