如何将JavaScript Integer变量作为主键传递到使用Modalform软件包的Django URL中



我正在使用datatables(https://datatabers.net/(。单击每一行时,必须将ID列的值作为打开更新表单的主要键传递。

除了将JavaScript变量传递到Django URL外,一切正常。

var adresid = 0
$('.dt-edit').each(function () {
    $(this).on('click', function(evt){
    $this = $(this);
    adresid = parseInt($this.parents('tr')[0].cells[13].innerHTML)
    })
    .modalForm({
       formURL: "{% url 'adres-update-modal' adresid %}"
    });
});

我试图将.replace()方法用作formURL: "{% url 'adres-update-modal' pk %}".replace('pk', adresid),但这不起作用。

这是我一直遇到的错误:

找不到" adres-update-modal"的" adres-update-modal"('',('。1 尝试:['adres/(?p [0-9] (/updatemodal/$']

当我用整数替换URL中的adresid(例如16(,一切都很好。

任何帮助将不胜感激

编辑:这是我的表的html

 <table id="masterTable" class="table nowrap" style="width:100%">
                    <thead>
                    <tr>
                        <th></th>
                        <th></th>
                        <th>A</th>
                        <th>B</th>
                        <th>C</th>
                        <th>D</th>
                        <th>E</th>
                        <th>F</th>
                        <th>G</th>
                        <th>H</th>
                        <th style="text-align:center;width:100px;">Add adres <button type="button" data-func="dt-add" class="btn btn-success btn-xs dt-add modal-adres-create">
                            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                        </button></th>
                        <th class="none">I</th>
                        <th class="none">J</th>
                        <th class="none">K</th>
                        <th class="none">L</th>
                        <th class="none">M</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for X in Y %}
                    {% for P in Q %}
                    {% if X.Z == U %}
                    <tr>
                        <td></td>
                        <td></td>
                        <td>A</td>
                        <td>B</td>
                        <td>C</td>
                        <td>D</td>
                        <td>E</td>
                        <td>F</td>
                        <td>G</td>
                        <td>H</td>
                        <td>
                            <button type="button" class="btn btn-primary btn-xs dt-edit" style="margin-right:16px;">
                                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                            </button>
                            <button type="button" class="btn btn-danger btn-xs dt-delete">
                                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                            </button>
                        </td>
                        <td>I</td>
                        <td>J</td>
                        <td>K</td>
                        <td>L</td>
                        <td>M</td>
                    </tr>
                    {% endif%}
                    {% endfor %}
                    {% endfor %}
                    </tbody>
                </table>

编辑2:要澄清,当我使用console.log(adresid)时,它确实返回具有16值的整数。因此,我不希望该问题在JavaScript部分以外的其他任何地方,该值必须传递给formURL值。

假设adresid = parseInt($this.parents('tr')[0].cells[13].innerHTML)返回所需的值,我本来可以期望modalForm方法位于on回调中,类似的是:

$('.dt-edit').each(function () {
  $(this).on('click', function(evt){
    $this = $(this);
    adresid = parseInt($this.parents('tr')[0].cells[13].innerHTML);
    $('.adres-update-modal').modalForm({
       formURL: "{% url 'adres-update-modal' adresid %}"
    });
  });
});

这将是获得单击行

获得正确值的唯一方法

相关内容

最新更新