在 Django url 模板标签中获取 javascript 变量的值 [英] Get javascript variable's value in Django url template tag

查看:47
本文介绍了在 Django url 模板标签中获取 javascript 变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,有一种使用 django 模板标签url"定向到 URL 的 DRY 方式,例如

It is known that there's a DRY way of directing to an URL by using django template tag "url", say

{% url "someview" arg1=X %}

在这里,我希望X"是一个 javascript 变量的值,比如 tmp.但以下不起作用

Here, I want "X" to be the value of a javascript variable, say tmp. But the following doesn't work

<script>
    ...{% url "someview" arg1=tmp %}...
</script>

我应该如何获取模板标签内的值?

How should I get the value inside a template tag?

推荐答案

我发现了一个在大多数情况下可能有效的技巧:

I found a trick that might work under most circumstances:

var url_mask = "{% url 'someview' arg1=12345 %}".replace(/12345/, tmp.toString());

它很干净,而且不违反 DRY 原则.

It's clean, and it doesn't break DRY principle.

这篇关于在 Django url 模板标签中获取 javascript 变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆