如何获取 POST 请求值? Django 中的列表?

发布于 2024-10-10 05:18:23 字数 409 浏览 1 评论 0原文

假设我要传递的参数名为printdata,并且printdata=['a', 'b', 'c']

我使用 "input type="hidden" name="alist" value={{printdata}}>" 来传递 参数。但是,当我尝试检索参数时 使用以下代码的views.py:

params = request.POST

params["alist"] 等于['a', 而不是['a', 'b', 'c']。我怀疑 django 使用 , 标识参数的结尾。

您有什么建议或任何其他方法来传递参数吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

莫相离 2024-10-17 05:18:23

在 HTML 中使用多个隐藏字段,每个值对应一个隐藏字段:

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

在服务器端,使用 request.POST.getlist 以列表形式读取所有值:

alist = request.POST.getlist('alist')
# 'alist' now contains ['1', '2', '3']

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']
各自安好 2024-10-17 05:18:23

确保在 HTML 中的列表字符串两边加上引号。 IE:

<input type="hidden" name="alist" value="{{printdata}}">

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

<input type="hidden" name="alist" value="{{printdata}}">
纵情客 2024-10-17 05:18:23

用于

request.POST.getlist('alist')

获取值。

Use

request.POST.getlist('alist')

to get the values.

不寐倦长更 2024-10-17 05:18:23
alist = request.POST.get('alist')
alist = request.POST.get('alist')
月光色 2024-10-17 05:18:23

例如,如果您在index.html中提交POST请求值列表,如下所示:

{# "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>

然后,您可以获取POST请求值' my_app1/views.py 中的列表,如下所示。 *根据我的实验,您无法在 Django 模板和 我的回答解释了如何在 Django 和 我的回答解释了如何获取POST 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')

另外,如果您在 index.html 中提交 POST 请求值列表,如下所示:

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

那么,您可以获取 POST my_app1/views.py 中的 请求值列表如下所示:

# "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')

然后,您可以在 test.html 中获取 POST 请求值列表如下图所示:

{# "test.html" #}

{{ request.POST.fruits }} {# apple,orange #}
{{ request.POST.dict }} {# {'csrfmiddlewaretoken': 'y2yGzMMJq2kVXGLUZ68JJxpNbYpFxZyRcjbOJxbQH5OsqJg8RaY1T3pQvo2Bpv7F', 'fruits': 'apple,orange'} #}

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'} #}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文