使用表单集创建指向记录的链接

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

我正在使用Django版本3.0.1,Python版本3.6.9

我正在尝试在表单集内的循环中为单个记录设置url,但是url标记即使在添加第二行之前将其打印在第一个表格单元中也看不到我的变量:

{# This displays correctly the integer id #}
<td>{{ form.record_id.value }}</td>

{# This next line doesn't work, though the above TD does display the record_id #}
<td><a href="{% url 'record_detail' form.record_id.value %}">{{ form.name.value }}</a></td>

我得到的错误是页面呈现时的错误:

Reverse for 'record_detail' with arguments '(None,)' not found. 1 pattern(s) tried: ['myapp\\/record_detail\\/(?P<pk>[0-9]+)\\/$']

这是我的模板表代码(请注意第二行已注释掉,否则在查看页面时会收到错误消息:]

<table id="formset" class="form">
{% for form in recordFormset.forms %}

  <tr class="someclass">
    <td>{{ form.record_id.value }}</td>

    {# This next line doesn't work, though the above TD does display the record_id #}
    {# <td><a href="{% url 'record_detail' form.record_id.value %}">{{ form.name.value }}</a></td> #}

  </tr>
{% endfor %}
</table>

我猜这是因为URL是在表单集的表单的for循环之前创建的,但是我该如何创建需要创建的这些URL?

((为简洁起见,请注意,我在上面的代码段中省略了可编辑表单字段)。

python django templates url
1个回答
0
投票

我最终采取了完全不同的方法。我创建了一个在表单字段中使用的小部件,该小部件具有一个带有超链接的模板。在呈现页面之前,我在视图中填充了链接的数据。

© www.soinside.com 2019 - 2024. All rights reserved.