如何将javascript函数的值传递给Django视图? [英] How pass javascript function 's value to Django view?

查看:71
本文介绍了如何将javascript函数的值传递给Django视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模板中,我有一些像这样的代码:

In template I have some code like this:

<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创建表格可以添加许多课程,然后在我应该提交它以进行视图的其他步骤并将所有课程保存在数据库中时.我有一个问题,如果有人帮助我,我会很高兴.

I want to add some new courses for selected student and for doing it, I create form which get course idnumber and course score.At first, when I fill form, javascript create table when I click on "add" button and I can add many courses then when I should submit it to do other step in view part and save all course in database. Ihave some problem I'm be happy if some one help me.

1)如何将股票"数组(javaScript中的全局数组,包括创建的表中的所有课程)发送到Django视图?

1)How to send "stock" array (which is global array in javaScript and including all of the course in created table) to Django view?

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

2)How clean form after press "add" button?

我为我的英语不好对不起.

I'm so sorry for my bad english.

推荐答案

向后发送数据的技巧是将POST请求发送回服务器.

Hi the trick to sending data back is to send POST request back to the server.

我将使用JQuery作为示例(因为它更快,更容易)

Im going to use JQuery as an example to do so (since it is so much faster and easier)

$.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以匹配您的后端,并将其指向实例的正确视图

Then you will have to adjust your apps' urls.py to match your postendpoint and point it to the correct view for instance

##urls.py
urlpatterns = [
    url(r'^postendpoint/$', views.YourViewsHere),
    ....
] 

最后,在您的视图中,您可以访问这样的帖子数据

and finally your in your views you can access the post data like this

##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对象,您可以查看许多属性和方法.

as you can request is a python object there are many attributes and methods that you can look at.

有关更多信息,请参见

1)Django请求响应 https://docs.djangoproject.com/zh-CN/1.7/ref/request-response/2)Django Ajax示例:如何将Ajax与Django应用程序集成?/a>

1) Django request-response https://docs.djangoproject.com/en/1.7/ref/request-response/ 2) Django Ajax exmple :How do I integrate Ajax with Django applications?

请注意,您将不得不使用我给您的代码.我认为这将是从那里学习的最好方法.

Note that you will have to play around with the code that I gave you. I think it will be the best way to learn from there.

我希望您觉得这有用

这篇关于如何将javascript函数的值传递给Django视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆