简体   繁体   English

如何在javascript中运行python脚本并在Django中刷新视图

[英]How to run a python script inside javascript and refresh the view in django

I have a python script that takes in some data and manipulates it. 我有一个python脚本,可以接收一些数据并对其进行操作。 However i need it to run on the client side inside the javascript to process some data and refresh the view. 但是我需要它在javascript内部的客户端上运行以处理一些数据并刷新视图。

The python file that manipulates data works well as I have tested it inside the IDLE shell 操纵数据的python文件运行良好,因为我已在IDLE外壳中对其进行了测试

 DataManipulation.py

class ModifyData(object):

       #Bunch of functions to manipulate data

below is the function used to render the view with url '.../test' which also works perfectly. 下面是用于使用url'... / test'呈现视图的函数,该函数也可以完美地工作。

 views.py

  def test(request):

        template = 'app/test.html'

        file = 'documents/Sample.csv' #File  to be loaded 

        args = {'test': file }

        return render(request, template, args)

After loading this page, i use a javascript library that displays the data on the page in a table, the user can then manipulate the data like multiply a column by 3, but where I am stuck is how to take my DataManipulation.py file to modify the data and updates the page with the updated column on a button click 加载此页面后,我使用了一个JavaScript库来在表格中的页面上显示数据,然后用户可以对数据进行操作,例如将一列乘以3,但是我遇到的问题是如何将DataManipulation.py文件保存到修改数据并在单击按钮时使用已更新的列更新页面

I think you should pass the data into template, and then use javascript to manipulate the data, after that you can use ajax to update your page without refresh, example: 我认为您应该将数据传递到模板中,然后使用javascript处理数据,之后可以使用ajax无需刷新即可更新页面,例如:

<!--html-->
<button onclick="deleteUser({{ request.user.pk }})">Submit</button>
<!---->

function deleteUser(userid) {

    var post_data = {
        'userid': userid,
    }

    $.ajax({
        type: "POST",
        url: "/deleteuser",// the view function to post
        data: post_data,
        contentType: 'application/json;charset=UTF-8',
        success: function(result) {
            // do something after post
            ...
        }
    });
}

the view function: 查看功能:

# url(r'^/deleteuser$', views.delete_user)
def delete_user(request):
    if request.method == 'POST':
        # do something
        userid = request.POST.get('userid')
        user = User.objects.get(pk=userid)
        # dict contain response data
        response_data = {...}

        # return response
        return HttpResponse(json.dumps(response_data),content_type='application/json')

Since executing python client side is not an option, you have two options. 由于执行python客户端不是一个选项,因此您有两个选择。

  1. re-write ModifyData in Javascript and use it client side. 用Javascript重新编写ModifyData并在客户端使用它。

  2. When a user does something like multiply a column by 3: 当用户执行诸如将某列乘以3的操作时:

    • Use client side JS to make an ajax request to the server 使用客户端JS向服务器发出Ajax请求
    • Have your server call ModifyData, and then return the data to the client 让您的服务器调用ModifyData,然后将数据返回给客户端
    • have client update view with new data 具有新数据的客户端更新视图

I would recommend porting your python code to JS, but if that isn't possible #2 will always work. 我建议将您的python代码移植到JS,但是如果不可能的话,#2将始终有效。 How you implement will just depend on how you manage data in the client. 实现方式仅取决于您如何在客户端中管理数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM