Reputation: 43
I am trying to develop some basic django templates where i initially pass the django template variable which is an array - {{array_list}}. I am able to perform operations on this dtv easily. But I am unable to post this variable back to views. Eg. I pass {'array_list': [1,2,3,4]}
<form action="some_action" method="post">
{% csrf_token %}
<input type="submit" value="sort">
<input type="hidden" name="array_list" value={{array_list}}>
</form>
And in views.py:
array_list = request.POST['array_list']
return render(request, 'result.html', {'array_list': array_list})
But i don't get the full array back to result.html, and i only get [1, as the array_list.
Upvotes: 0
Views: 644
Reputation: 51948
Probably you can do something like this:
First, use join
tag to turn that list into comma separated string.
<form action="some_action" method="post">
{% csrf_token %}
<input type="submit" value="sort">
<input type="hidden" name="array_list" value='{{array_list|join:","}}'>
</form>
And get the value in POST request and split it by ,
.
array_list = request.POST['array_list'].split(',')
Upvotes: 1