首页 > 解决方案 > Javascripts not working after Django update

问题描述

I updated my Django 1.9 application to Django 3.1 following the changes mentioned in the releases. The application is running, but the javascripts are not showing the same bevhavior.

<a href="{% url field.label_tag|cut:field.label|striptags prj_pk=ProjectId %}" class="add-another" id="add_{{field.auto_id}}" onclick="return showAddAnotherPopup(this);"></a>

This line previously showed + button to open a new form but now the + button is not seen on the webpage. What has changed with respect to Javascripts in Django 3.1?

标签: javascriptdjango

解决方案


Django 无法正确呈现 HTML 标记中的 javascript。原因是 Javascript 是静态的,当它直接放在 HTML 模板中时,Django 会将其视为 HTML 并可能不正确地呈现它。因此,使用单独的静态 js 文件创建一个函数以使用 jquery 进行点击调用。

$("#some-id").click(function() {
     //do something here
})

推荐阅读