简体   繁体   English

使用 Django(模板)在 html 元素上动态运行 js

[英]run js on html element dynamically with Django(template)

I want to run price_update() function on h6 element我想在 h6 元素上运行price_update() function

{% for products in allProducts %}
<div class="col-6"> 
    Final Price: <h6 id="final_price_{{product.slug}}" onload="price_update({{products.price}},{{products.offer}}, {{product.slug}})"></h6>                                    
</div>

<script type="text/javascript">
function price_update(price, offer, slug) {
    let discounted_price = price- parseInt(price * offer / 100);
    let id = "final_price_" + String(slug);
    let f_price = document.getElementById(id);
    f_price.innerText = discounted_price;                                   
}
</script>
{% endfor %}

I got to know that onload cannot be used with <h6> .我知道onload不能与<h6>一起使用。 How can update innerText of <h6> for every product?如何为每个产品更新<h6>的 innerText ?

Create a custom template tag , do the arithmetic calculations there and, get the result:创建一个自定义模板标签,在那里进行算术计算,得到结果:

  • In one of your apps create a new folder that is named templatetags在您的一个应用程序中创建一个名为templatetags的新文件夹
  • paste in the folder the __init__.py file to ensure the directory is treated as a Python package.__init__.py文件粘贴到文件夹中,以确保该目录被视为 Python package。
  • In templatetags folder, create a new module.templatetags文件夹中,创建一个新模块。 This module is the one that would be loaded in your template.该模块是将加载到您的模板中的模块。 something like: myapp_extras.py类似于: myapp_extras.py
  • Import desired models (product) and then, add register = template.Library() to ensure the the module is a valid tag library导入所需的模型(product) ,然后添加register = template.Library()以确保模块是有效的标签库
  • Register the custom filter by:通过以下方式注册自定义过滤器:

register the filter注册过滤器

@register.filter(name='price_update')
def price_update(product_id):
    # Get the product by id, and calculate the price
    
    # Return updated price

Load the module {% load myapp_extras %} in the template then, in your template use price_update tag:在模板中加载模块{% load myapp_extras %}然后,在您的模板中使用price_update标签:

<div class="col-6"> 
    Final Price: <h6 id="final_price_{{product.slug}}" >{{product.id|price_pudate}}</h6>                                    
</div>
  1. Solution https://stackoverflow.com/a/63391216/14020019 by Amin Mir absolutely correct but I've implemented the solution in different way. Amin Mir的解决方案https://stackoverflow.com/a/63391216/14020019绝对正确,但我以不同的方式实施了解决方案。

  2. My Solution : I've added property in my model我的解决方案:我在 model 中添加了属性

    @property def final_price(self): f_price = self.price - (self.price * self.offer // 100) return f_price

    using this I directly used the final_price using .使用这个我直接使用final_price使用. operator, ie product.final_price运算符,即product.final_price

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

相关问题 在 django(表单)模板中复制 html 元素的值 - Copy value of html element in django (forms) template JS / HTML —动态创建带有选项的select元素 - JS/HTML — dynamically create select element with options 使用JS从html元素动态获取ElementId - Dynamically acquire the ElementId from an html element with JS 使用HTML元素作为“模板”还是在javascript中动态生成HTML更好? - Is it better to use an HTML element as a “template” or to generate the HTML dynamically in the javascript? 如何使用 Z9E13B69D1D2DA927102ACAAAFZ15 在 HTML 页面中插入 Django 模板标签 + HTML 元素 - How to insert a Django template tag + HTML element in a HTML page with Javascript 如何从django模板动态更改html中的下拉菜单 - how to dynamically change dropdown menu in html from django template 使用应用程序名称和名称空间在Django模板URL中动态添加js变量值 - dynamically add js variable value in django template url with appname and namespace 在Django HTML模板中包含html,css,js小部件 - Include html, css, js widget in Django html template 如何复制“contenteditable”元素的值并将其插入其他html模板? 姜戈 - how to copy the value of a 'contenteditable' element and insert it to other html template? Django 访问Django模板{%url%}中的HTML表单元素 - Accessing HTML form element inside a Django template {% url %}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM