【发布时间】:2021-06-17 09:33:13
【问题描述】:
我正在使用 django 模板循环访问 hp_tracker 对象。每个 hp_tracker 都有自己的 js 代码,用于通过 ajax 更改值。我几乎可以肯定我应该在 .js 文件中包含此代码,但是我很难找到一种方法来唯一地选择每个 hp_tracker 而无需访问 .js 文件中的 django 模板变量。所以...我将 js 移到了 html 模板本身。我知道这无疑会产生巨大的安全隐患,我也愿意讨论这些问题。
不管怎样,我的问题。我的 js 失败了,因为我声明了不唯一的全局变量(!)。它们被用来控制一些计数器和 setTimeout,我不知道现在如何使用它们来做我想做的事情。
所以 for 循环试图一遍又一遍地重新声明同一个变量。在脚本的其余部分中,我使用的是 JQuery,它非常乐意使用 django 变量 {{ hp_tracker.id }},但 javascript 不是因为变量名称中不允许使用对象 ID 中的“-”字符.
我到底应该怎么做才能解决这个热点问题。有没有办法在没有全局变量的情况下运行我的代码?我可以在不使用对象 ID 的情况下识别我的 for 循环中的对象吗?
<div id="ToolSessionPageWrapper">
<div class="tool-session-page-header">
<div id=OpenToolSelectionMenuBtn class="arrow-down"></div>
</div>
<div class="tool-session-page-body">
<div id="HpTrackersViewWrapper" class="tool-body">
{% for hp_tracker in hp_trackers %}
<div class="hp-tracker-box">
<div id="{{hp_tracker.id}}-HpTrackerTitle" class="hp-tracker-title">{{ hp_tracker.title }}</div>
<br />
<form method="POST" action="{% url 'hp_change_value' hp_tracker.id %}" id="{{ hp_tracker.id }}-HpValueForm">
{% csrf_token %}
<div class="hp-control-box">
<button type="button" id="{{ hp_tracker.id }}-HpValueDecreaseBtn" class="hp-value-change-btn decrease">-</button>
<div id="{{hp_tracker.id}}-HpValue" class="hp-value">{{ hp_tracker.hp_value }}</div>
<div id="{{ hp_tracker.id }}-HpValueInput">{{ hp_change_value_form.hp_value }}</div>
<div id="{{ hp_tracker.id }}-HpChangeValueCover" class="hp-value hp-change-value-cover"></div>
<div id="{{ hp_tracker.id }}-HpChangeValue" class="hp-value hp-change-value"></div>
<button type="button" id="{{ hp_tracker.id }}-HpValueIncreaseBtn" class="hp-value-change-btn increase">+</button>
</div>
</form>
</div>
<script>
{#ajax call for HpValueForm#}
function changeHpValue(form) {
'use strict';
$(form).submit(function (e) {
// preventing from page reload and default actions
e.preventDefault();
// serialize the form data.
let serializedData = $(form).serialize();
// make POST ajax call
$.ajax({
type: 'POST',
url: '{% url 'hp_change_value' hp_tracker.id %}',
data: serializedData,
success: function (response) {
let form_instance = JSON.parse(response['form_instance']);
let fields = form_instance[0]['fields'];
$('#{{hp_tracker.id}}-HpValue').empty().prepend(fields.hp_value);
console.log('ajaxSuccess');
},
error: function (response) {
console.log(response["responseJSON"]["error"]);
}
});
});
}
{#control timeout before hp_value increase or decrease is submitted#}
let {{ hp_tracker.id|escapejs }}hp_add_subtract_value = $('#{{hp_tracker.id}}-HpValue').text(),
hp_change_value = 0,
timeoutHandler;
function timeoutControl() {
clearTimeout(timeoutHandler);
timeoutHandler = setTimeout(function () {
$('#{{ hp_tracker.id }}-HpValueInput input').val(hp_add_subtract_value);
$('#{{ hp_tracker.id }}-HpValueForm').submit();
$('#{{hp_tracker.id}}-HpChangeValue').css({'display': 'none'});
$('#{{hp_tracker.id}}-HpChangeValueCover').css({'display': 'none'});
hp_change_value = 0;
$('#{{hp_tracker.id}}-HpChangeValue').empty().prepend(hp_change_value);
}, 2000);
}
{#increase the hp value#}
$('#{{ hp_tracker.id }}-HpValueIncreaseBtn').click(function (e) {
'use strict';
hp_add_subtract_value++;
hp_change_value++;
$('#{{hp_tracker.id}}-HpChangeValue').css({'display': 'inline'});
$('#{{hp_tracker.id}}-HpChangeValueCover').css({'display': 'inline'});
$('#{{hp_tracker.id}}-HpChangeValue').empty().prepend(hp_change_value);
timeoutControl();
});
{#decrease the hp value#}
$('#{{ hp_tracker.id }}-HpValueDecreaseBtn').click(function (e) {
'use strict';
hp_add_subtract_value--;
hp_change_value--;
$('#{{hp_tracker.id}}-HpChangeValue').css({'display': 'inline'});
$('#{{hp_tracker.id}}-HpChangeValueCover').css({'display': 'inline'});
$('#{{hp_tracker.id}}-HpChangeValue').empty().prepend(hp_change_value);
timeoutControl();
});
{#submit HpValueForm#}
$('#{{ hp_tracker.id }}-HpValueForm').on('submit', changeHpValue('#{{ hp_tracker.id }}-HpValueForm'));
</script>
{% endfor %}
</div>
</div>
【问题讨论】:
-
查看
{% for上的文档。您可以执行{{ forloop.counter }}之类的操作来获取当前循环索引。这应该可以帮助您创建唯一的名称 - docs.djangoproject.com/en/3.1/ref/templates/builtins/#for -
啊,是的,我没想到。我去看看!
-
你好,有必要把django代码放在jquery里面吗?
-
我在 jQuery 中使用 Django 变量作为选择器来识别由 Django 模板系统动态生成的元素。不过,我对所有想法都持开放态度!
标签: javascript jquery django templates