Я хочу передать данные из пользовательского ввода в шаблон create_proposal.html. Форма находится внутри динамически сгенерированной таблицы с помощью JavaScript. В каждой строке есть ячейки с входными данными. Таблица внутри create_proposal.html выглядит следующим образом:
<table id="empTable" class="table-striped" border="1" cellmargin="100px" cellpadding="0px"cellspacing="5px">
<tr>
<th>
<h5></h5>
</th>
<th>
<h5>No.</h5>
</th>
<th>
<h5>Part no.</h5>
</th>
<th style="width:30vw">
<h5>Description</h5>
</th>
<th>
<h5>Quantity</h5>
</th>
<th>
<h5>Unit Market price</h5>
</th>
<th>
<h5>Markup percentage</h5>
</th>
</tr>
</table>
И скрипт, который генерирует строки:
<script>
// ARRAY FOR HEADER.
var arrHead = new Array();
arrHead = ['', 'No.', 'Part no.', 'Description', 'Quantity', 'Unit market price', 'Markup percentage'];
// ADD A NEW ROW TO THE TABLE.s
function addRow() {
var empTab = document.getElementById('empTable');
var rowCnt = empTab.rows.length; // GET TABLE ROW COUNT.
var tr = empTab.insertRow(rowCnt); // TABLE ROW.
tr = empTab.insertRow(rowCnt);
for (var c = 0; c < arrHead.length; c++) {
var td = document.createElement('td'); // TABLE DEFINITION.
td = tr.insertCell(c);
if (c == 0) { // FIRST COLUMN.
// ADD A BUTTON.
var button = document.createElement('input');
// SET INPUT ATTRIBUTE.
button.setAttribute('type', 'button');
button.setAttribute('value', 'Remove');
button.setAttribute('class', 'btn btn-danger');
// ADD THE BUTTON's 'onclick' EVENT.
button.setAttribute('onclick', 'removeRow(this)');
td.appendChild(button);
}
else if(c == 1){
var ele = document.createElement('input');
ele.setAttribute('type', 'number');
ele.setAttribute('id', 'id_item_no');
ele.setAttribute('name', 'item_no');
td.appendChild(ele);
}
else if(c == 2){
var ele = document.createElement('input');
ele.setAttribute('type', 'text');
ele.setAttribute('id', 'id_part_no');
ele.setAttribute('name', 'part_no');
td.appendChild(ele);
}
else if(c == 3){
var ele = document.createElement('input');
ele.setAttribute('type', 'text');
ele.setAttribute('id', 'id_description');
ele.setAttribute('name', 'description');
td.appendChild(ele);
}
else if(c == 4){
var ele = document.createElement('input');
ele.setAttribute('type', 'number');
ele.setAttribute('id', 'id_quantity');
ele.setAttribute('name', 'quantity');
td.appendChild(ele);
}
else if(c == 5){
var ele = document.createElement('input');
ele.setAttribute('type', 'number');
ele.setAttribute('id', 'id_unit_market_price');
ele.setAttribute('name', 'unit_market_price');
td.appendChild(ele);
}
else if(c == 6){
var ele = document.createElement('input');
ele.setAttribute('type', 'number');
ele.setAttribute('name', 'markup_percentage');
ele.setAttribute('id', 'id_markup_percentage');
td.appendChild(ele);
}
}
}
// DELETE TABLE ROW.
function removeRow(oButton) {
var empTab = document.getElementById('empTable');
empTab.deleteRow(oButton.parentNode.parentNode.rowIndex); // BUTTON -> TD -> TR.
}
</script>
в моем views.py Я вызываю функцию с именем merge (), которая принимаетпараметры из пользовательского ввода. Функция в view.py, которая вызывает функцию merge (), выглядит следующим образом:
def resultPage(request):
if request.method == 'POST':
form =createNewFinancial(request.POST)
if form.is_valid():
name = form.cleaned_data['file_name']
organization = form.cleaned_data['organization_name']
project_type = form.cleaned_data['project_type']
reference = form.cleaned_data['reference_number']
our_ref_no = form.cleaned_data['our_reference_number']
date = form.cleaned_data['date']
item_no = form.cleaned_data['item_no']
part_no = form.cleaned_data['part_no']
description = form.cleaned_data['description']
quantity = form.cleaned_data['quantity']
unit_market_price = form.cleaned_data['unit_market_price']
markup_percentage = form.cleaned_data['markup_percentage']
merge(name, organization, project_type, reference, our_ref_no, date, item_no, part_no, description, quantity, unit_market_price, markup_percentage)
return render(request, 'result.html')
и функция merge ():
def merge(name, organization, project_type, reference, our_reference, date, item_no, part_no, description, quantity, unit_market_price, markup_percentage):
template = 'master.docx'
document = MailMerge(template)
unit_price = calculateUitPrice(unit_market_price, markup_percentage)
total_price = quantity * unit_price
document.merge(
organization = organization,
project_type = project_type,
reference = reference,
our_ref_no = our_reference,
date = date,
)
item_table = [{
'item_no' : str(item_no),
'part_no' : str(part_no),
'description' : str(description),
'quantity' : str(quantity),
'unit_price' : str(unit_price),
'total_price' : str(total_price)
},]
document.merge_rows('item_no', item_table)
document.write(name+'.docx')
есть несколько строк, в которых есть поля ввода стот же идентификатор. Я хотел передать значения в массиве, но не знаю как. Любое рабочее решение тоже подойдет. Спасибо