Как передать значение из django html в django views
Я хочу сделать раздел с товарами, в котором можно "+" и "-" увеличивать или уменьшать количество товара. А затем, в зависимости от количества, "продавать" его.
Теперь нам нужно передать значение {{ el.id }} в django views
Мой код: html:
<form method="POST">
{% csrf_token %}
{% for el in form3 %}
{% if el.count > 0 %}
[ {{ el.count }}шт. ] <br>
[ {{ el.title }} ]<br>
<a id="minus{{ el.id }}" href="#"><b>[ - ]</b></a>
<span id="value{{ el.id }}">0</span>
<a id="plus{{ el.id }}" href="#"><b>[ + ]</b></a>
<br>
Function where i + or - from count
<script>
$(function(){
var valueElement = $('#value{{ el.id }}');
function incrementValue(e){
valueElement.text(Math.max(parseInt(valueElement.text()) + e.data.increment, 0));
return false;
}
$('#plus{{ el.id }}').bind('click', {increment: 1}, incrementValue);
$('#minus{{ el.id }}').bind('click', {increment: -1}, incrementValue);
});
</script>
{% endif %}
{% endfor %}
</form>
как я могу получить значения с помощью "span id=value{{ el.id }}"
Решено
{% for el in form3 %}
{% if el.count > 0 %}
[ {{ el.count }}шт. ] <br>
[ {{ el.title }} ]<br>
<a id="minus{{ el.id }}" href="#"><b>[ - ]</b></a>
<span id="span-value{{el.id}}">0</span>
<a id="plus{{ el.id }}" href="#"><b>[ + ]</b></a>
<br>
<input type="hidden" name=name-{{el.id}} id="input-{{el.id}}" value="0">
<script>
$(function(){
var valueElement = $('#span-value{{el.id}}');
function incrementValue(e){
valueElement.text(Math.max(parseInt(valueElement.text()) + e.data.increment, 0));
var value = $("#span-value{{el.id}}").text();
var lower=$("#input-{{el.id}}").val(value);
return false;
}
$('#plus{{ el.id }}').bind('click', {increment: 1}, incrementValue);
$('#minus{{ el.id }}').bind('click', {increment: -1}, incrementValue);
});
</script>
{% endif %}
{% endfor %}
Используйте это для копирования\дублирования значения span на входе После этого вы можете использовать request.post.dict или request.post.items() для key:values