Python Django管理-连续输出执行命令

Python Django管理-连续输出执行命令,python,ajax,django,Python,Ajax,Django,我有一个脚本,我正在为它构建一个界面,这样人们可以在上传CSV文件后执行。我能够执行所有操作并使脚本正常运行,但如何显示连续输出?我应该对代码做哪些更改 以下是我的档案: scripts.html-脚本在此执行,并通过AJAX调用在服务器上执行。脚本执行完毕后,输出被放入div#Output <div class="table_container"> <form method="post" enctype="multipart/form-data" data-ajax=

我有一个脚本,我正在为它构建一个界面,这样人们可以在上传CSV文件后执行。我能够执行所有操作并使脚本正常运行,但如何显示连续输出?我应该对代码做哪些更改

以下是我的档案:

scripts.html-脚本在此执行,并通过AJAX调用在服务器上执行。脚本执行完毕后,输出被放入div#Output

<div class="table_container">
    <form method="post" enctype="multipart/form-data" data-ajax="false">{% csrf_token %}
        <h4>Rebilling</h4>
        <div class="clear"></div>
        <img class="loading-gif" src="{{ STATIC_URL }}img/loading-clear.gif" alt="" />
        <table>
            <tbody>
            <tr>
                <td style="width: 180px;"><label>Upload the CSV</label></td>
                <td>
                    <input type="hidden" name="script_type" value="renew_subscriptions">
                    <input type="file" name="csv_file" />
                </td>
            </tr>
            <tr>
                <td style="width: 180px;"></td>
                <td>
                    <input type="submit" name="Execute" />
                </td>
            </tr>
            </tbody>
        </table>
    </form> 
</div>

<h2>Script Output</h2>
<div id="output">
    {% autoescape off %}

    {% endautoescape %}
</div>

<script type="text/javascript">
    // Variable to store your files
    var files;

    // Add events
    $('input[type=file]').on('change', prepareUpload);

    // Grab the files and set them to our variable
    function prepareUpload(event)
    {
      files = event.target.files;
    }

    $('form').on('submit', submitForm);

    // Catch the form submit and upload the files
    function submitForm(event)
    {
        event.stopPropagation(); // Stop stuff happening
        event.preventDefault(); // Totally stop stuff happening
        $("#output").html("");

        var form = $(this);
        form.find(".loading-gif").css("display", "block");
        form.find("input[type='submit']").prop('disabled', true);

        // Create a formdata object and add the files
        var data = new FormData(form.get(0));

        $.ajax({
            url: '/crm/scripts',
            type: 'POST',
            data: data,
            cache: false,
            dataType: 'html',
            processData: false,
            contentType: false,
            success: function(data)
            {
                // console.dir(data);
                $("#output").html(data);
            },
            error: function(jqXHR, textStatus, errorThrown)
            {
                // Handle errors here
                console.log('ERRORS: ' + textStatus);
            },
            complete: function()
            {
                form.find(".loading-gif").css("display", "none");
                form.find("input[type='submit']").prop('disabled', false);
            }
        });


        return false;
    }
</script>
只需输出连续逐行显示即可。非常感谢您的帮助

干杯, 泽伊

“网络请求是一个可怕的地方,你想尽快进出 尽你所能”——瑞克·布兰森

您在这里所做的是创建一个架构问题。基本上,在编写CSV文件时,您正在创建额外的磁盘IO您正在web请求中执行此操作。这不是一个好主意

然而,这也是你所描述的问题的关键

快速n脏: 您可以从django管理命令中获取返回值。将其作为数据传递回jquery的ajax调用的success方法

然而:请不要这样做

您需要一个异步任务系统来完成该csv文件的写入。此外,您还希望将数据写入某个地方(dbms/nosql),以便您的网页可以通过()监听这些数据。这不是一项微不足道的任务,但最终结果是值得付出努力的。下面是一些解决这类问题的方法

构建异步任务分配/排队系统

  • 芹菜用的中间商
  • //
对该数据进行轮询

  • 你是什么
本pycon讲座涵盖了这些技术


那么您的问题到底是什么?你给我们看的代码有什么问题?它不起作用吗?它是否会产生意外行为?抱歉,刚刚编辑了此帖子以包含一个问题。我的问题本质上是,我应该改变什么,这样我才能有一个连续的输出来显示?@Cyber这说明了什么?
def all_scripts(request):    # Accounts page
    # c = {}
    script_type = None
    csv_file = None
    out = StringIO()

    if request.is_ajax and request.method == 'POST':
        csv_file = request.FILES.get('csv_file')

        if csv_file:
            # print "over here"
            ### write the csv_file to a temp file
            tup = tempfile.mkstemp() # make a tmp file
            f = os.fdopen(tup[0], 'w') # open the tmp file for writing
            f.write(csv_file.read()) # write the tmp file
            f.close()

            ### return the path of the file
            filepath = tup[1] # get the filepath
            # print filepath

            if 'script_type' in request.POST:
                script_type = request.POST['script_type']
                if script_type == "change_credit":
                    credit_amount = None

                    if 'credit_amount' in request.POST:
                        credit_amount = request.POST['credit_amount']

                    if 'function' in request.POST:
                        function = request.POST['function']

                        if function == "remove":
                            management.call_command(script_type, filepath, credit_amount, remove=[True], stdout=out)
                        else:
                            management.call_command(script_type, filepath, credit_amount, stdout=out)

                elif script_type == "renew_subscriptions":
                    management.call_command(script_type, filepath, verbosity=1, interactive=False, stdout=out)

                print out.getvalue()
                return HttpResponse(out.getvalue())

    return render_to_response('crm/scripts.html', context_instance=RequestContext(request))