我有以下 django 模板(http://IP/admin/start/分配给一个名为 view 的假设 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
是 View 中引用的 Django 模型的 objects.all()
。每当单击“开始”提交输入时,我希望“开始” View 在返回呈现页面之前在函数中使用 {{ source.title}}
数据。如何将已发布的信息(在本例中为隐藏输入)收集到 Python 变量中?
最佳答案
了解您的 View 收到的请求对象:https://docs.djangoproject.com/en/dev/ref/request-response/#httprequest-objects
您的隐藏字段还需要一个可靠的名称,然后是一个值:
<input type="hidden" name="title" value="{{ source.title }}">
然后在一个 View 中:
request.POST.get("title", "")
关于python - Django - 从 POST 请求中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11336548/