Django, How can i use {% url %} template in jQuery, prepend?


$.each(data, function (index, value) { #Here is where 'value' from
$("#contentsbox")
.prepend(
'<div><a id="URLcomes" href="{% url 'somename' value.id %}">Here comes hyperlinks</a></div>'
);
});

我有一些 json 数据,并希望动态制作div标签。

我将使用 .prepend((,但需要纯文本,我不能用{% url 'somename' value.id %}制作字符串

在该模板标签上,value.id 来自each- 函数(索引,value(

我想在 prepend(( 中{% url 'somename' value.id %},但我无法根据需要将其设置为字符串prepend()

如何制作字符串?

试试这个, 在你的 html 中,把

<div id="element-id" url="{% url 'somename' value.id %}" style="display : none;"></div>

在脚本之前。 在你的jquery中,

var str = $('#element-id').attr('url');
//str will give you the desired string. 

希望这有帮助。谢谢。

最新更新