2014-11-24 72 views
2

在模板中我有一些像这样的代码:如何将javascript函数的值传递给Django视图?

<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> 

我要添加到选定的学生一些新的课程和这样做,我创建获得课程编号和课程成绩的形式。首先,当我填写表单时,当我点击“添加”按钮时,JavaScript创建表格,然后我可以添加很多课程,然后当我提交它在视图部分中执行其他步骤并保存全部数据库课程。 我有些问题,如果有人帮助我,我会很开心。

1)如何向Django视图发送“stock”数组(包括javaScript中的全局数组,并包括创建的表中的所有课程)到Django视图?

2)按下“添加”按钮后如何清洁表格?

我很抱歉我的英语不好。

+0

尝试http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp或jqery AJAX http://api.jquery.com/jquery.ajax/ – madzohan 2014-11-24 09:01:34

+0

使用AJAX!更多信息在这里:http://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications – seddonym 2014-11-24 09:09:28

回答

1

嗨发送回数据的诀窍是发送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和点它例如

##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的阿贾克斯exmple:How do I integrate Ajax with Django applications?

请注意,你将不得不玩的,我给你的代码。我认为这将是从那里学习的最佳方式。

我希望你发现这很有

+0

非常感谢您的回答,我认为这是有帮助的,但我不知道JQuery和起初,我想我必须学习它:)谢谢。 – user3789719 2014-11-24 10:52:23

+0

无论如何你能接受我的答案吗?欢呼=) – biobirdman 2014-11-24 10:53:13

+0

你可以选择不使用JQuery,并使用香草JavaScript的后通话,但概念是相同的。请记住在javascript中将数据添加到您的请求中,并通过访问请求对象从django访问您的数据 – biobirdman 2014-11-24 10:54:38

相关问题