简体   繁体   English

在JavaScript中的反向网址中传递参数

[英]passing a parameter in the reverse url in javascript

javascript JavaScript的

   var url = '{% url cand_resume "cnd_id" %}';   
   url = url.replace("cnd_id",id);
   cell2.innerHTML= '<a href="' + url + '"> View</a>';

id is the variable (1000) id是变量(1000)

urls.py urls.py

       url(r'^(?P<cnd_id>\d*)/resume/', 'download_resume',name='cand_resume'),

it throws this error: Caught NoReverseMatch while rendering: Reverse for 'cand_resume' with arguments '(u'cnds_id',)' and keyword arguments '{}' not found. 它将引发以下错误:渲染时捕获了NoReverseMatch:反向使用参数'(u'cnds_id',)'和关键字参数'{}'的'cand_resume'。

I guess when you parse your url 我想您解析网址时

var url = '{% url cand_resume "cnd_id" %}';  

you sending a string cnd_id which will not match your url. 您发送的字符串cnd_id与您的网址匹配。

try (not sure if you need quite around your function name) 试试(不确定您是否需要函数名称)

var url = '{% url cand_resume 1000 %}'; 

or something like (id is variable from django) 或类似的东西(id是django中的变量)

var url = '{% url cand_resume id %}'; 

You can try dirty trick like 您可以尝试像

var url = '{% url cand_resume 1000 %}'.replace (1000, cnd_id);

or check out this lib https://github.com/mlouro/django-js-utils 或查看此库https://github.com/mlouro/django-js-utils

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM