Django - 从POST请求获取值 [英] Django - taking values from POST request
问题描述
我有以下django模板(http:// IP / admin / start /被分配给一个称为视图的假设视图):
I have the following django template (http://IP/admin/start/ is assigned to a hypothetical view called 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 %}
来源
是视图中引用的Django模型的 objects.all()
。每当点击开始提交输入时,我希望开始视图在返回渲染页面之前使用函数中的 {{source.title}}
数据。如何收集信息POST(在这种情况下,在隐藏的输入)到Python变量?
sources
is the objects.all()
of a Django model being referenced in the view. Whenever a "Start" submit input is clicked, I want the "start" view to use the {{ source.title}}
data in a function before returning a rendered page. How do I gather information POSTed (in this case, in the hidden input) into Python variables?
推荐答案
阅读有关请求对象您的意见收到: https://docs.djangoproject.com/ en / dev / ref / request-response /#httprequest-objects
Read about request objects that your views receive: https://docs.djangoproject.com/en/dev/ref/request-response/#httprequest-objects
此外,您的隐藏字段需要一个可靠的名称,然后是一个值:
Also your hidden field needs a reliable name and then a value:
<input type="hidden" name="title" value="{{ source.title }}">
然后在视图中:
request.POST.get("title", "")
这篇关于Django - 从POST请求获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!