Django管理 - 执行连续输出命令
问题描述:
我有一个脚本,我正在构建一个接口,以便人们可以在上传CSV文件后执行。我能够执行所有操作并让脚本正常运行,但是如何显示连续输出?我应该对我的代码做些什么改变?Django管理 - 执行连续输出命令
下面是我的文件:
scripts.html - 脚本都是从这里执行,并通过Ajax调用服务器上执行。一旦脚本完成执行,输出就放入div#输出。
<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>
views.py - 阿贾克斯在这里发送,并且指令通过Django的管理执行
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))
只需要输出由线不断线显示。任何帮助深表感谢。
干杯, Zee的
答
“web请求是一个可怕的地方,你想进入和离开一样快 ,你可以” - 里克·布兰森
你做了什么这里创建了一个架构问题。基本上你在编写你的CSV文件时创建了额外的磁盘IO。 您正在网络请求中执行此操作。'不是个好主意'。
但它也是你所描述的问题的关键。
快速ñ脏: 你可以得到一个Django管理命令like so返回值。把它回传给jquery的ajax调用的成功方法作为你的数据。
但是:请不要那样!
您需要一个异步任务系统才能将该csv文件写入。此外,您想要将数据写入某处(dbms/nosql),以便您的网页可以通过(polling, streaming or websockets)进行监听。这不是一件小事,但最终的结果非常值得您付出努力。这里有一些proven django-stack choices来解决这类问题。
构建异步任务处理/排队系统
轮询数据
这PYCON演讲包括这些技术。
那么究竟什么是你的问题?你向我们展示的代码有什么问题?它不起作用吗?它会产生意外的行为吗? – CoryKramer 2014-10-10 11:47:24
对不起,只是编辑帖子,包括一个问题。我的问题基本上是,我要改变什么才能让连续的输出显示出来? – zee 2014-10-10 12:06:17
@Cyber能否澄清? – zee 2014-10-10 12:33:05