2

I'm using ajax and django for dynamically populate a combo box. ajax component works really fine and it parse the data to the view but int the view, when i'm using the spiting function it gives me a exception called "Value Error:need more than 1 value to unpack ". can anyone helps me to figure out the error :) :) code:

def dropdownPopulate(request):



if request.method=='POST' :
    key = request.POST['id']
else:
    key=""



level, tree_id=key.split(",")

next_nodes=Structure.objects.filter(tree_id=key[tree_id]).filter(level=key[level])
context={'name':next_nodes}     
return render_to_response('renderAjax.html',context)    
himanka
  • 23
  • 1
  • 6

3 Answers3

1

This is because s.split(',') is returning list of length 1:

level, tree_id = key.split(',')

Make sure it return list of length 2:

parts = key.split(',')
if len(parts) == 2:
    level, tree_id = parts
elif len(parts) == 1:
    level = parts[0]
    tree_id = None
else:
    # do something
    level = tree_id = None
    pass

The apply filter like this:

next_nodes = Structure.objects.all()
if level:
    next_nodes = next_nodes.filter(level=level)
if tree_id:
    next_nodes = next_nodes.filter(tree_id=tree_id)
Aamir Rind
  • 38,793
  • 23
  • 126
  • 164
  • the data coming from the ajax is like this [u'1', u'0'] so i want to get the numerical values only by removing the Unicode values how can i do that ??? – himanka May 15 '13 at 10:54
0

Probably error occurs at this line:

level, tree_id=key.split(",")

It is needed to handle the situation, when key will not have ",". Or maybe it will have more than one ",".

Look at your code:

if request.method=='POST' :
    key = request.POST['id']
else:
    key=""

It is possible, that key will be a blank string.

Here are examples, when error can occur:

1.

>>> key = ""
>>> level, tree_id=key.split(",")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack

2.

>>> key = "a,b,c"
>>> level, tree_id=key.split(",")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

Only this will be fine (when it is only one ","):

>>> key = "a,b"
>>> level, tree_id=key.split(",")
>>> 
stalk
  • 11,934
  • 4
  • 36
  • 58
  • the data coming from the ajax is like this [u'1', u'0'] so i want to get the numerical values only by removing the Unicode values how can i do that ??? – himanka May 15 '13 at 10:54
  • In that case you need to unescape your data: `import HTMLParser`; `h = HTMLParser.HTMLParser(`); `print h.unescape("[u'1', u'0']")`. The result will be `"[u'1', u'0']"`. Look [this thread](http://stackoverflow.com/questions/2087370/decode-html-entities-in-python-string) – stalk May 15 '13 at 11:00
0

You have multiple problems.

level, tree_id=key.split(",")

This will fail, as key may not have ,, so split will not return 2 values.

next_nodes=Structure.objects.filter(tree_id=key[tree_id]).filter(level=key[level])

Here you are accessing key as dict, which is incorrect as it is string.

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • the data coming from the ajax is like this [u'1', u'0'] so i want to get the numerical values only by removing the Unicode values how can i do that ??? – himanka May 15 '13 at 10:55
  • @himanka, I think you need to fix ajax not to send values in that form. There seems values in single quotes in url encoded form. – Rohan May 15 '13 at 11:02