【问题标题】:use javascript variable in django template在 django 模板中使用 javascript 变量
【发布时间】:2014-07-15 19:04:06
【问题描述】:

我有一个自定义模板标记,它通过对 SOAP 服务的 Web 调用检索国家列表并填充 html select 标记。现在我有了另一个模板标签,它显示给定国家/地区的选项列表,很明显,它将国家名称作为参数。因此,只有在 onchange 事件在 html 选择标签上触发并且我将国家名称作为用户选择的 javascript 变量后,我才能将国家名称传递给第二个自定义标签。我如何将此值传递给自定义模板标签? 这是我的自定义标签

from mezzanine import template
from suds.client import Client
register = template.Library()

@register.as_tag
def get_countries(*args):
    url = 'http://www.sendfromchina.com/shipfee/web_service?wsdl'
    client = Client(url)
    countries = client.service.getCountries()
    countries = map(lambda x: x._enName, countries)
    return countries

@register.as_tag
def get_available_carriers(weight,country,length,width,height):
    url = 'http://www.sendfromchina.com/shipfee/web_service?wsdl'
    client = Client(url)
    rates = client.service.getRates(weight,country,length,width,height)
    rates=map(lambda x: (x._shiptypecode, x._totalfee), rates)
    return rates

这是我的html 选择标签

<select id='countrylist' onchange="getOption(this)">
    {% get_countries as countries %}
    {% for country in countries %}
        <option>{{ country }}</option>
    {% endfor %}
<select>

最后,这是我的 javascript

<script type="text/javascript">
function getOption(sel){
    var country = sel.value;
    {% get_available_carriers 1 country 10 10 10 as carriers %}
    console.log('{{ carriers }}')
}
</script>

我似乎无法将 country js 变量传递给 get_available_carriers 标记

非常感谢任何帮助!谢谢

【问题讨论】:

    标签: javascript jquery django django-templates


    【解决方案1】:

    Django 模板是在服务器端构建的,在页面生成时,JavaScript 在客户端执行,当需要时。 Thus, Django and Javascript can't share objects/data.

    在您的页面中,使用当前的 Javascript,您将拥有如下内容:

    <script type="text/javascript">
    function getOption(sel){
        var country = sel.value;
                                    // Empty line due to the templatetag
        console.log('')
    }
    </script>
    

    您需要在视图中生成列表并返回已构建的carrier 对象。运气好的话,你也许可以在 Javascript 中使用它。

    这里最好的方法仍然是发出 AJAX 请求来获取这个列表:

    def get_available_carriers(request, weight, country, length, width, height):
        url = 'http://www.sendfromchina.com/shipfee/web_service?wsdl'
        client = Client(url)
        rates = client.service.getRates(weight,country,length,width,height)
        rates=map(lambda x: (x._shiptypecode, x._totalfee), rates)
    
        return json.dumps(rates)
    

    然后用 jQuery 获取它(如果你正在使用它):

        $.get('{% url "get_available_carriers" 1 country 10 10 10 %}', function(data){
            console.log(data);
        });
    

    不要忘记定义 URL 模式,在我的示例中使用 get_available_carriers

    【讨论】:

      【解决方案2】:

      您没有将值从 javascript 函数传递到 django 模板标签。但在这种情况下,您可以使用 ajax 调用。

      http://www.tangowithdjango.com/book/chapters/ajax.html

      https://bradmontgomery.net/blog/2008/11/24/a-simple-django-example-with-ajax/

      更新:

      看到这个你就明白是怎么回事了。

      How to pass javascript variable to django custom filter

      希望这是一个有用的想法。

      【讨论】:

      • 非常感谢@dhana 的回答,我现在正在阅读您的链接资源。如果我有更多问题,我会回来。同时,再次感谢您的宝贵帮助,对不起,我无法为您的答案投票
      • 这几乎是一个仅链接的答案...下次要小心,并尝试使用相关且有用的资源来扩展您的帖子。
      • 嗨@MaximeLorant 他提出任何想法。我给出了我的想法和参考参考链接以供学习。 stackoverflow 有什么问题吗?。
      • 如果前两个链接有一天消失了,您的答案将不再有用。该政策是在答案本身中引用页面的相关部分,或者更好地为当前案例调整页面示例。您确实回答了“为什么会失败?”这个问题。但问题是“如何?”仅通过链接回答。见stackoverflow.com/help/how-to-answer
      猜你喜欢
      • 2016-02-02
      • 2021-06-14
      • 2021-06-19
      相关资源
      最近更新 更多