【问题标题】:Get javascript variable's value in Django python url template tag在 Django python url 模板标签中获取 javascript 变量的值
【发布时间】:2016-05-30 03:14:55
【问题描述】:

我有一个 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 链接传递,这样一旦用户单击该链接,他就会重定向到该特定记录详细信息视图。

我尝试如下:

<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 %}"}}'); 应该做什么?
  • 将带有我的 id 的 url 链接传递给我的模板(td 单元格)

标签: javascript jquery python html django


【解决方案1】:

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

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

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

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

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

这将更改标签内所有锚标签的 html,该标签以 foo 为 id。

【讨论】:

    猜你喜欢
    • 2013-07-23
    • 2021-02-15
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2019-07-31
    相关资源
    最近更新 更多