[英]calling django url through javascript with parameters
This is my javascript function where I pass parameters, using alert I checked that function is getting the parameters I want but I am unable to pass them in django url other wise giving a string the url works but not with parameters.这是我的 javascript 函数,我在其中传递参数,使用 alert 我检查了该函数正在获取我想要的参数,但我无法在 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
}
I have checked the value getting is string the way I want but how to pass it?我已经按照我想要的方式检查了获取的值是字符串,但如何传递它?
kindly try this:请试试这个:
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>
使用确切的网址,即new / value1 / value2 /
Use用
location.href = "/" + v
where v is your id or slug其中 v 是您的 id 或 slug
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.