1

I'm a newer to Python, and I am having a problem with Django's forms:

  args="[('job_201404181748_1712666','job_201404181748_1712666')]"
  jobid = forms.ChoiceField(choices=args)

This raised ValueError: need more than 1 value to unpack. If I replace args with a string, then it works ok:

 jobid = forms.ChoiceField(choices=[('job_201404181748_1712666','job_201404181748_1712666')])

I don't know why. Can anyone help?

Will
  • 24,082
  • 14
  • 97
  • 108
lanyun
  • 141
  • 1
  • 4
  • 10
  • Try this :- jobid = forms.ChoiceField(choices=eval(args)) – Tanveer Alam Apr 18 '14 at 11:01
  • 1
    yes,you're right @TanveerAlam – lanyun Apr 18 '14 at 11:09
  • the args is build by the for loop. @TanveerAlam – lanyun Apr 18 '14 at 11:10
  • Here 'eval' function just evaluates your string 'args' to List. – Tanveer Alam Apr 18 '14 at 11:13
  • While I am sure it could work, I would advise against arbitrary use of the `eval` function. It is seen as quick&dirty and could have some nasty side effect, especially in web applications dealing with user input. I never had to resort to using `eval`, and I am confident that @lanyun could write his code in a way that avoids it as well. – Roy Prins Apr 18 '14 at 11:17

2 Answers2

4

Try args without the surrounding quotes. By surrounding it with quotes, you make args into a string. The ChoiceField expect a list of choices.

in short, make line 33 look like:

 args=[('job_201404181748_1712666','job_201404181748_1712666')]
Roy Prins
  • 2,790
  • 2
  • 28
  • 47
1

You need to remove the quotes

args=[('job_201404181748_1712666','job_201404181748_1712666')]
yprez
  • 14,854
  • 11
  • 55
  • 70
dhana
  • 6,487
  • 4
  • 40
  • 63