从js脚本检索输入值到django视图



我以这种方式在js脚本中创建了一个html输入:

var compte = document.createElement("input");
compte.setAttribute("name", "compte");

我需要将该输入值检索到 django 视图中。通常,我使用输入的 name 属性:

num=request.POST['compte']

在这种情况下,知道 html 输入是使用 javascript 创建的是行不通的。那么我如何获得值呢?

我在整个代码下面添加。我实际上插入了一行包含表的输入的新行。

<form class="myForm" action="Plan_comptable" method='POST'
enctype="multipart/form-data">
<table id="myTable" class="gtreetable table gtreetable-fullAccess">
{% csrf_token %}
<thead><tr><th>Compte</th><th>Intitulé</th><th></th>
</tr></thead>
<tbody>
{% for el in ViewPC_query %}
<tr>
<td>{{ el.Num }}</td>
<td>{{ el.Intitule }}</td>
<td width="30">
<div class="dropdown">
<button class="btn btn-primary 
dropdown-toggle" type="button" data-toggle="dropdown">Action
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a id="{{ forloop.counter }}" class="dropdown-item" 
onclick="beforeFunction(this);" href="#">Créer avant</a></li>
<li><a id="{{ forloop.counter }}" class="dropdown-item" 
onclick="afterFunction(this);" href="#">Créer après</a></li>
<li><a class="dropdown-item" onclick="" href="#">Créer first child</a> 
</li>
<li id="actionsep"><a class="dropdown-item" href="#" 
id="actionsep">Créer last child</a></li>
<li><a class="dropdown-item" href="Modifier_PC/{{ el.id }}">Modifier</a> 
</li>
<li><a class="dropdown-item" href="Supprimer_PC/{{ el.id 
}}">Supprimer</a></li>
</ul></div></td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
<script>
function beforeFunction (element) {
var myTable = document.getElementById("myTable");
var currentRow = myTable.insertRow(element.id);
var compte = document.createElement("input");
compte.setAttribute("id", "idcompte");
compte.setAttribute("name", "compte");
compte.setAttribute("class", "form-control");
var intitule = document.createElement("input");
intitule.setAttribute("name", "intitule");
intitule.setAttribute("class", "form-control");
var button = document.createElement("button");
button.setAttribute("name", "action" + element.id);
button.setAttribute("class", "btn btn-primary dropdown-toggle");
button.setAttribute("data-toggle", "dropdown");
var textnode1 = document.createTextNode("Action");
var textnode2 = document.createTextNode("Enregistrer");
var textnode3 = document.createTextNode("Annuler");
var span = document.createElement("span");
span.setAttribute("class", "caret");
var ul = document.createElement("ul");
ul.setAttribute("class", "dropdown-menu");
var li1 = document.createElement("li");
var li2 = document.createElement("li");
var Enregistrer = document.createElement("a");
Enregistrer.setAttribute("href", "UpdatePC_before");
var Supprimer = document.createElement("a");
Supprimer.setAttribute("href", "{% url 'comptes' %}");
var div = document.createElement("div");
div.setAttribute("class", "dropdown");
var currentCell = currentRow.insertCell(-1);
currentCell.appendChild(compte);
currentCell = currentRow.insertCell(-1);
currentCell.appendChild(intitule);
Enregistrer.appendChild(textnode2);
Supprimer.appendChild(textnode3);
li1.appendChild(Enregistrer);
li2.appendChild(Supprimer);
ul.appendChild(li1);
ul.appendChild(li2);
button.appendChild(textnode1);
button.appendChild(span);
currentCell = currentRow.insertCell(-1);
div.setAttribute("class", "dropdown");
div.appendChild(button);
div.appendChild(ul);
currentCell.appendChild(div);
}

似乎您正在创建输入,但没有将其附加到表单中。

document.querySelector('form.formClassName').appendChild(compte)

最新更新