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?