gpt4 book ai didi

javascript - 在 Django python url 模板标签中获取 javascript 变量的值

转载 作者:行者123 更新时间:2023-11-28 01:16:44 24 4
gpt4 key购买 nike

我有一个django模板部分

{% for l in items%}

<td style="text-align: center"
id="{{m.id}}_{{m.set|slice:':7' }}_{{m.kidu}}_{{l}}">
</td>
{% endfor %}

和下面的 JS 部分:

<script>

$(document).ready(function() {
{% for a in all_results %}
$('#{{a.id}}_{{a.set|slice:":7"}}_{{ a.kidu}}_{{ a.adi|slice:":2" }}').html('{{ a.id }}');
$('#{{a.id}}_{{a.set|slice:":7"}}_{{ a.kidu}}_{{ a.adi|slice:":2" }}').addClass('{{ a.state__name.split|join:"_" }}');
{% endfor %}
});
</script>

我正在尝试将 id 作为 href 链接传递到我的模板中,这样一旦用户单击该链接,他就会重定向到该特定记录的详细信息 View 。

我试过如下:

<script>

$(document).ready(function() {
{% for a in all_results %}
$('#{{a.id}}_{{a.set|slice:":7"}}_{{ a.kidu}}_{{ a.adi|slice:":2" }}').html('{{ href="{% url 'req_detail_view' a.id %}"}}');
$('#{{a.id}}_{{a.set|slice:":7"}}_{{ a.kidu}}_{{ a.adi|slice:":2" }}').addClass('{{ a.state__name.split|join:"_" }}');
{% endfor %}
});
</script>

但我得到的是 Could not parse the remainder: '="{% url 'req_detail_view' a.id %}"' from 'href="{% url 'req_detail_view' a.id %}"' 有什么想法吗?提前致谢

最佳答案

只需删除 {{ , }} .你的代码应该是,

.html("href=\"{% url 'req_detail_view' a.id %}\"");

.html("<a href=\"{% url 'req_detail_view' a.id %}\">Click here</a>");

.html是用于将 html 设置为特定标记的 jquery/javascript 函数。例如,

$(#foo a).html("<a href=\"{% url 'req_detail_view' a.id %}\">Click here</a>");

这将更改具有 foo 的标签中存在的所有 anchor 标签的 html作为 id。

关于javascript - 在 Django python url 模板标签中获取 javascript 变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35473272/

24 4 0