如何在Django中获取POST请求值?

问题描述 投票:0回答:5

我有以下 django 模板(http://IP/admin/start/ 被分配给一个名为 view 的假设视图):

{% for source in sources %}
  <tr>
    <td>{{ source }}</td>

    <td>
    <form action="/admin/start/" method="post">
      {% csrf_token %}
      <input type="hidden" name="{{ source.title }}">
      <input type="submit" value="Start" class="btn btn-primary">
    </form>
    </td>

  </tr>
{% endfor %}

sources
是视图中引用的 Django 模型的
objects.all()
。每当单击“开始”提交输入时,我希望“开始”视图在返回渲染页面之前使用函数中的
{{ source.title}}
数据。如何将发布的信息(在本例中为隐藏输入中)收集到 Python 变量中?

python django post http-post httprequest
5个回答
177
投票

了解您的视图收到的请求对象:https://docs.djangoproject.com/en/dev/ref/request-response/#httprequest-objects

您的隐藏字段还需要一个可靠的名称和一个值:

<input type="hidden" name="title" value="{{ source.title }}">

然后在视图中:

request.POST.get("title", "")

15
投票

如果您需要在前端执行某些操作,您可以响应表单的 onsubmit 事件。如果您只是发布到 admin/start,您可以通过请求对象访问视图中的发布变量。 request.POST 这是 post 变量的字典


5
投票

您可以使用:

request.POST['title']

它将轻松获取具有该标题的数据。


4
投票

对于 django 表单,你可以这样做;

form = UserLoginForm(data=request.POST) #getting the whole data from the user.
user = form.save() #saving the details obtained from the user.
username = user.cleaned_data.get("username") #where "username" in parenthesis is the name of the Charfield (the variale name i.e, username = forms.Charfield(max_length=64))

0
投票

例如,如果您提交以下

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="meat" value="beef" /></br>
  <input type="submit" />
</form>

然后,您可以在

POST
中获取
views.py
请求值,如下所示。 *我的回答解释了如何在Django中获取
GET
请求值:

# "views.py"

from django.shortcuts import render

def test(request):

    print(request.POST['fruits']) # apple
    print(request.POST.get('meat')) # beef
    print(request.POST.get('fish')) # None
    print(request.POST.get('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST.getlist('fruits')) # ['apple']
    print(request.POST.getlist('fish')) # []
    print(request.POST.getlist('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST._getlist('meat')) # ['beef']
    print(request.POST._getlist('fish')) # []
    print(request.POST._getlist('fish', "Doesn't exist")) # Doesn't exist
    print(list(request.POST.keys())) # ['csrfmiddlewaretoken', 'fruits', 'meat']
    print(list(request.POST.values())) # ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'apple', 'beef']
    print(list(request.POST.items())) # [('csrfmiddlewaretoken', 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'), ('fruits', 'apple'), ('meat', 'beef')]
    print(list(request.POST.lists())) # [('csrfmiddlewaretoken', ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS']), ('fruits', ['apple']), ('meat', ['beef'])]
    print(request.POST.dict()) # {'csrfmiddlewaretoken': 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'fruits': 'apple', 'meat': 'beef'}
    print(dict(request.POST)) # {'csrfmiddlewaretoken': ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'], 'fruits': ['apple'], 'meat': ['beef']}

    return render(request, 'test.html')

然后,您可以在

POST
中获取
test.html
请求值,如下所示:

{# "test.html" #}

{{ request.POST.fruits }} {# apple #}
{{ request.POST.meat }} {# beef #}
{{ request.POST.dict }} {# {'csrfmiddlewaretoken': 'Vzjk89LPweM4loDWTb9gFNHlRQNJRMNwzQWsiUaWNhgBOr8aLfZyPjHobgqFJimk', 'fruits': 'apple', 'meat': 'beef'} #}
© www.soinside.com 2019 - 2024. All rights reserved.