如何从客户端Java脚本中执行Django uri模板标签? [英] How to execute Django uri template tags from within client side Java Script?

查看:25
本文介绍了如何从客户端Java脚本中执行Django uri模板标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们的java模板中有一个url标记时,它会在单击时执行 {%url'product_details'pk = soproduct.id%} ,它用于超级链接,目标将是包含域和路径的完整路径.

When we have a url tag in our java template it executes on a click {% url 'product_details' pk=soproduct.id %} and it it is used inside the hyper link the destination will be full path including domain and path.

<a href="{% url 'soproduct_details'   pk=soproduct.id %}">{{ soproduct.id }}</a> 

工作完美并生成

http://127.0.0.1:8000/production/subproduct/details/7/

但是,如果我们想在客户端Java脚本

var destination = {% url 'product_details'   pk=soproduct.id %}

存在一个问题,即将生成的链接将包含路径,但不包含域

there is a problem that the link that will be generated will include the path but will not include the domain

/production/subproduct/details/7/

/production/subproduct/details/7/

该如何解决?

更新:

这是我尝试执行的脚本,位于Django模板的for循环中.

This is the script I try to execute it is located in the for loop in the django template .

<script>


var currentLocation = window.location.href;

function AddCardToTrello() {
  Trello.addCard({
  url: currentLocation,
  name: "{{ soproduct.product }}",
  due: {{ soproduct.required_date|date:"SHORT_DATE_FORMAT"  }}
});
}

    function addCardThenGo(url) {
        AddCardToTrello();
        window.location.href = url;
    }
</script>

在脚本之后我马上打电话

And right after the script I have my call

<a onclick="addCardThenGo({% url 'add_to_production_board' pk=soproduct.id uri=request.build_absolute_uri %})">Add to Trello</a>

因此,在使用新参数的循环中,脚本将在代码中重复执行多次

So script will be repeated in the code as many times as loop goes with new parameters

推荐答案

哦,我想我明白了.如果您提供的代码与您使用的代码完全相同,则您会忘记引号.应该是这样的:

Oh, I think I got it. If the code you provided is exactly the same you are using, then you forgot about quotes. It should be like this:

var destination = "{% url 'product_details'   pk=soproduct.id %}";

然后您可以执行以下操作:

And after that you can make this:

window.location.href = destination;

更新

请尝试执行以下操作:

<a onclick="addCardThenGo('{% url 'add_to_production_board' pk=soproduct.id uri=request.build_absolute_uri %}')">Add to Trello</a>

这篇关于如何从客户端Java脚本中执行Django uri模板标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆