在模板中我有一些这样的代码:
<div id="form">
<form name="myForm" action="Http://localhost:8000/student/student_add_course/" onclick="return addTable();" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
<div id="form-data">
{{ form.course_id }}{{ form.course_id.errors }}
<label for="id_course_id">:Course Id number:</label><br>
{{ form.score }}{{ form.score.errors }}
<label for="id_score">Course score:</label><br>
<p><input type="button" value="add" /></p>
<p><input type="submit" value="submit" /></p>
</div>
</form>
</div>
<div id="table">
<table id="TABLE" border = '1'>
<tr>
<th>id number</th>
<th>score</th>
</tr>
<tr>
<td id="id_number"></td>
<td id="score"></td>
</tr>
这是“脚本”部分:
<script type="text/javascript">
var stock = new Array();
var i = 0;
function addTable() {
var id = document.forms["myForm"]["course_id"].value;
var score = document.forms["myForm"]["score"].value;
stock[i] = new Array(id, score);
//Get the table that shows the selected course from html code
var table = document.getElementById('TABLE');
//Add id and score to row of the table which is inside the html code.
if (document.getElementById("id_number").innerHTML=="" || document.getElementById("score").innerHTML=="")
{document.getElementById("id_number").innerHTML=id;
document.getElementById("score").innerHTML=score;}
//Create table row and append it to end of above table
else{var tr = document.createElement('TR');
for (j = 0; j < 2; j++) {
var td = document.createElement('TD')
td.appendChild(document.createTextNode(stock[i][j]));
tr.appendChild(td)
}
table.appendChild(tr);
}
i=i+1;
return stock;
}
</script>
我想为选定的学生添加一些新类(class),为此,我创建了获取类(class) ID 号和类(class)分数的表格。首先,当我填写表格时,当我单击“添加”按钮时,javascript 创建表格,然后我可以添加许多类(class),然后当我应该提交它以在 View 部分执行其他步骤并将所有类(class)保存在数据库中时。 我遇到了一些问题,如果有人帮助我,我会很高兴。
1)如何将“库存”数组(它是 javaScript 中的全局数组,包括创建表中的所有类(class))发送到 Django View ?
2)按下“添加”按钮后表格如何清理?
我很抱歉我的英语不好。
最佳答案
您好,发送回数据的技巧是将 POST 请求发送回服务器。
我将以 JQuery 为例(因为它更快更容易)
$.ajax({
// points to the url where your data will be posted
url:'/postendpoint/$',
// post for security reason
type: "POST",
// data that you will like to return
data: {stock : stock},
// what to do when the call is success
success:function(response){},
// what to do when the call is complete ( you can right your clean from code here)
complete:function(){},
// what to do when there is an error
error:function (xhr, textStatus, thrownError){}
});
然后你将不得不调整你的应用程序的 urls.py 以匹配你的 postendpoint 并将它指向正确的 View ,例如
##urls.py
urlpatterns = [
url(r'^postendpoint/$', views.YourViewsHere),
....
]
最后,在您看来,您可以像这样访问帖子数据
##views.py
def YourViewsHere(request):
if request.method == 'GET':
do_something()
elif request.method == 'POST':
## access you data by playing around with the request.POST object
request.POST.get('data')
正如您所要求的那样,它是一个 python 对象,您可以查看许多属性和方法。
更多信息请看
1) Django 请求-响应 https://docs.djangoproject.com/en/1.7/ref/request-response/ 2) Django Ajax 示例:How do I integrate Ajax with Django applications?
请注意,您将不得不尝试使用我提供给您的代码。我认为这将是从那里学习的最佳方式。
希望你觉得有用
关于javascript - 如何将 javascript 函数的值传递给 Django View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27100825/