简体   繁体   English

在JQuery append()中映射Django URL

[英]Mapping Django URLs in JQuery append()

I am appending an anchor tag dynamically after an AJAX call completes, but I'm not able to set the href attribute of it as a Django URL. 我将在AJAX调用完成后动态附加锚标记,但无法将其href属性设置为Django URL。 Here's my code: 这是我的代码:

$maincontent.append("<a id='resultslink'>Get results here</a>");
$('#resultslink').attr('href', "{% url 'results' %}");

Django gives the following error - Django给出以下错误-

The current path, InitialWebApp/hdfs/{%, didn't match any of these.

Apparently, Django thinks {% is the href value. 显然,Django认为{%href值。

To resolve the url using the url template tag requires that the template be rendered again which doesn't happen after your AJAX call. 要使用url模板标签解析url,需要再次渲染模板,这在您的AJAX调用后不会发生。

You can either hardcode the url or have the resolved url returned as part of the payload from the server for each AJAX call. 您可以对URL进行硬编码,也可以将解析的URL作为每个AJAX调用的服务器有效负载的一部分返回。

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

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