使用 Django 在 HTML 按钮上执行外部脚本

时间:2021-08-01 13:02:14

标签: python django

所以我正在实现一个包含不同 HTML 按钮的 Django 网页,每个 HTML 按钮执行不同的 Python 脚本。执行外部脚本的按钮会显示错误。 这是 HTML 代码

<form action="/external/" method="POST">
{% csrf_token %}

{{data_external}}<br><br>
{{data1}}
<br><br>
<input type="submit" value="Execute External Python Script">

</form>

这里是 views.py :


def external(request):
    inp= request.POST.get('param')
    out= run([sys.executable,'/scripts/test.py',inp],shell=False,stdout=PIPE)
    print(out)
    return render(request,'web_app/home.html',{'data1':out.stdout}) 

错误如下:

     Internal Server Error: /external/
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\User\cti\web_app\views.py", line 25, in external
    out= run([sys.executable,'scripts/test.py',inp],shell=False,stdout=PIPE)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 501, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 947, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1356, in _execute_child
    args = list2cmdline(args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 561, in list2cmdline
    for arg in map(os.fsdecode, seq):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\os.py", line 822, in fsdecode
    filename = fspath(filename)  # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not NoneType
[01/Aug/2021 14:37:44] "POST /external/ HTTP/1.1" 500 96366

我应该如何解决这个问题?

0 个答案:

没有答案