这是我的javascript函数,我在其中传递参数,使用警报,我检查该函数正在获取所需的参数,但无法以django url传递它们,否则将字符串传递给url,但不能与参数一起使用。
function myFunction(a) {
var v = a.value;
alert(v);
location.href="{% url 'new_event' v %}"; //does not works
location.href="{% url 'new_event' 'string' %}"; //works
}
我已经检查了获取值的方式是我想要的字符串,但是如何传递呢?
答案 0 :(得分:0)
请尝试一下:
function myFunction(a) {
var v = a.value;
alert(v);
location.href="{% url 'new_event' v %}"; // this will not work because your django **url** filter is pre-processed on your server while your javascript variable is processed on **client**
// if you want your variable v to be dynamic, you need to include it in your django's view **context**
location.href="{% url 'new_event' 'string' %}"; //works
}
// include in your views.py
context['v'] = 'some-string'
// template.html
<script>
function myFunction(a) {
var v = '{{ v }}'; // javascript variable v
alert(v);
location.href="{% url 'new_event' v %}"; // django context variable v
}
</script>
答案 1 :(得分:0)
使用确切的网址,即new / value1 / value2 /
答案 2 :(得分:0)
使用
location.href = "/" + v
其中 v 是您的 id 或 slug