0

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.

A.Yadav
  • 43
  • 1
  • 6

1 Answers1

1

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(',')
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • I am using your way of representing value but its giving : join requires 2 arguments, 1 provided. – A.Yadav Jan 21 '20 at 06:04
  • Thanks to your answer, i got to know about join and from documentation that you linked, I figured it out. value should be represented as vvalue={{array_list|join:","}}. Maybe, you can change your answer to this as it would be useful. – A.Yadav Jan 21 '20 at 06:13