简体   繁体   English

如何让 Django 代码中的 Javascript 监听和响应前端的点击事件?

[英]How do I make Javascript inside Django code listen & respond to on click events on the front end?

I have a page which has a list of products.我有一个包含产品列表的页面。 For each product, I have a button (Add to Cart).对于每个产品,我都有一个按钮(添加到购物车)。

Once a user clicks on this button the product gets added on a side panel (aka cart).一旦用户单击此按钮,产品就会添加到侧面板(又名购物车)上。 This side panel has:该侧面板具有:

  1. Quantities to be changed更改数量
  2. Delete icon - in case user wants to remove the product from the cart.删除图标 - 如果用户想从购物车中删除产品。 So, these delete icons are created run time every time a user adds the product to the cart.因此,每次用户将产品添加到购物车时,都会在运行时创建这些删除图标。

Now, I have an event listener in my JS which would listen to click on the delete icon and then remove the product.现在,我的 JS 中有一个事件侦听器,它会侦听单击删除图标然后删除产品。 But, any clicks on the delete icons are not getting responded.但是,对删除图标的任何点击都没有得到响应。

Funny thing is if I take the same JS code and use it in Chrome console it works.有趣的是,如果我采用相同的 JS 代码并在 Chrome 控制台中使用它,它就可以工作。 Any idea why this is not working ?知道为什么这不起作用吗?

From what I understand you have to make a folder called static at the root directory of your app this is where you keep your JavaScript/CSS files.据我了解,您必须在应用程序的根目录下创建一个名为 static 的文件夹,这是您保存 JavaScript/CSS 文件的地方。 Then you have to let Django know where your static files are in your settings.py file.然后你必须让 Django 知道你的静态文件在你的 settings.py 文件中的位置。 By adding the code showed below.通过添加如下所示的代码。 I have also included how to render in the static files in your HTML file hope this helps.我还包括如何在 HTML 文件中的静态文件中呈现希望这会有所帮助。

settings.py:设置.py:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

STATIC_URL = '/static/'

HTML file: HTML文件:

<!DOCTYPE html>
<head>
  {% load static %}
  <script src="{% static 'app.js' %}"></script>   
  <title>Site</title>
</head>
<body>
<img src="{% static 'img.png' %}" alt="Mon image" />
  {% block content %}{% endblock %}
</body>
</html>

You could change app.js and img.png depending on how you want to name your files.您可以根据您希望如何命名文件来更改 app.js 和 img.png。

There is a similar thread here: django how to include javascript in template这里有一个类似的线程: django how to include javascript in template

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

相关问题 我如何在 Shopware 6 Storefront 中监听和响应 JavaScript 事件 - How Do I Listen To And React To JavaScript Events In Shopware 6 Storefront 如何在前端镜像 Django 的电子邮件验证 - How do I mirror Django's email validation on the front end 如何将前端值传递给 JavaScript 计数器? - How do I pass a front end value to JavaScript counter? 我如何有多个类来监听Javascript中的click事件? - How do I have multiple classes to listen to the click event in Javascript? 使用 VueJS 2.0 和一个 vue-mdl 菜单组件,如何响应点击事件? - Using VueJS 2.0 and a vue-mdl menu component, how do I respond to click events? 如何强制固定定位元素内的元素响应Android Web浏览器上的javascript onclick事件? - How can I force elements inside a fixed positioned element respond to javascript onclick events on the Android web browser? 如何让 Plotly.js 监听刻度标签的点击事件? - How to make Plotly.js listen the click events of the tick labels? 如何在使用jQuery类型提交的按钮上收听点击事件? - How do I listen to click events on buttons with type submit using jQuery? 如何使用 javascript 监听 map 中的不同点击事件? - How to listen different click events in map using javascript? 我们如何让DataView控件响应javascript事件? - How can we make the DataView Control respond to javascript events?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM