How to get a POST request values' list in Django?
Asked Answered
S

5

2

Suppose the parameter I want to pass is called printdata, and printdata=['a', 'b', 'c'].

I use "input type="hidden" name="alist" value={{printdata}}>" to pass the parameter. However, when I try to retrieve the parameter in views.py using the following code:

params = request.POST

params["alist"] is equal to ['a', instead of ['a', 'b', 'c']. I suspect that django identifies the end of a parameters using ,.

Do you have any suggestions or any other methods to pass the parameter?

Solubilize answered 2/1, 2011 at 23:28 Comment(0)
U
8

Use multiple hidden fields, one for each value, in your HTML:

<input type="hidden" name="alist" value="1" />
<input type="hidden" name="alist" value="2" />
<input type="hidden" name="alist" value="3" />

On the server side, use request.POST.getlist to read all the values as a list:

alist = request.POST.getlist('alist')
# 'alist' now contains ['1', '2', '3']
Unfounded answered 3/1, 2011 at 0:29 Comment(0)
O
0

Make sure to put quotation marks around the list string in your HTML. ie:

<input type="hidden" name="alist" value="{{printdata}}">
Orissa answered 2/1, 2011 at 23:31 Comment(0)
A
0

Use

request.POST.getlist('alist')

to get the values.

Autochthonous answered 3/1, 2011 at 0:4 Comment(0)
H
0
alist = request.POST.get('alist')
Ho answered 19/5, 2016 at 16:19 Comment(0)
F
0

For example, if you submit the POST request values' list in index.html as shown below:

{# "index.html" #}

<form action="{% url 'my_app1:test' %}" method="post">
  {% csrf_token %}
  <input type="text" name="fruits" value="apple" /></br>
  <input type="text" name="fruits" value="orange" /></br>
  <input type="submit" />
</form>

Then, you can get the POST request values' list in my_app1/views.py as shown below. *According to my experiments, you cannot properly get the POST request values' list in Django Templates and my answer explains how to get a GET request values' list in Django and my answer explains how to get POST request values in Django:

# "my_app1/views.py"

from django.shortcuts import render

def test(request):

    print(request.POST.getlist('fruits')) # ['apple', 'orange']
    print(request.POST.getlist('meat')) # []
    print(request.POST.getlist('meat', "Doesn't exist")) # Doesn't exist
    print(request.POST._getlist('fruits')) # ['apple', 'orange']
    print(request.POST._getlist('meat')) # []
    print(request.POST._getlist('meat', "Doesn't exist")) # Doesn't exist
    print(list(request.POST.lists())) # [('csrfmiddlewaretoken', ['OE3hidxt0awKjRFXxUddvT6u0cRfhdMnsVGpsYWAhd0hMUabpY3vFp6xkCub9Jlb']), ('fruits', ['apple', 'orange'])]
    print(dict(request.POST)) # {'csrfmiddlewaretoken': ['OE3hidxt0awKjRFXxUddvT6u0cRfhdMnsVGpsYWAhd0hMUabpY3vFp6xkCub9Jlb'], 'fruits': ['apple', 'orange']}

    return render(request, 'index.html')

In addition, if you submit the POST request values' list in index.html as shown below:

<form action="{% url 'my_app1:test' %}" method="post">
  {% csrf_token %}
  <input type="text" name="fruits" value="apple,orange" />
  <input type="submit" />
</form>

Then, you can get the POST request values' list in my_app1/views.py as shown below:

# "my_app1/views.py"

from django.shortcuts import render

def test(request):

    print(request.POST['fruits'].split(',')) # ['apple', 'orange']
    print(request.POST.getlist('fruits')[0].split(',')) # ['apple', 'orange']
    print(request.POST._getlist('fruits')[0].split(',')) # ['apple', 'orange']
    print(list(request.POST.values())[1].split(',')) # ['apple', 'orange']
    print(list(request.POST.items())[1][1].split(',')) # ['apple', 'orange']
    print(list(request.POST.lists())[1][1][0].split(',')) # ['apple', 'orange']
    print(request.POST.dict()['fruits'].split(',')) # ['apple', 'orange']
    print(dict(request.POST)['fruits'][0].split(',')) # ['apple', 'orange']

    return render(request, 'index.html')

Then, you can get the POST request values' list in test.html as shown below:

{# "test.html" #}

{{ request.POST.fruits }} {# apple,orange #}
{{ request.POST.dict }} {# {'csrfmiddlewaretoken': 'y2yGzMMJq2kVXGLUZ68JJxpNbYpFxZyRcjbOJxbQH5OsqJg8RaY1T3pQvo2Bpv7F', 'fruits': 'apple,orange'} #}
Fairman answered 20/9, 2023 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.