1

I have an integer list x that I want to pass to a template via the context:

x = [3, 1, 4, 1, 5, 9]
context = {'x': x}
return render(request, 'mytemplate.html', context)

In the template, I then want to pass this list back to my view via POST:

<form>
    <input type="hidden" name="x" value="{{ x }}"/>
    <input type="submit"/>
</form>

Back in the view, I then want to access an element of this list:

x = request.POST['x']
y = x[3]

But this gives me the error:

list indices must be integers, not unicode

I presume that this is because everything passed via POST is always a unicode string, right? So how can I convert this string into an integer list? If it was just a single integer, rather than a list, I could write x = int(request.POST['x']); but how does this extend to a list?

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
  • Read this: http://stackoverflow.com/questions/19334374/python-converting-a-string-of-numbers-into-a-list-of-int – fixmycode Jul 05 '14 at 22:52

0 Answers0