繁体   English   中英

在模板中显示“python manage.py test”的结果

[英]Showing the result of "python manage.py test" inside template

我正在编写 views.py 函数,它应该返回“python manage.py test”并将其呈现给模板

视图.py

@login_required
def Test_Case(request):
     output = os.popen("python manage.py test").read()
     return render(request, 'Test-Case.html',{'output':output} )

上面的代码什么都不返回,但是当我这样做时

@login_required
def Test_Case(request):
     output = os.popen("dir").read()
     return render(request, 'Test-Case.html',{'output':output} )

它工作正常,有什么想法我该怎么做

感谢您的帮助

大多数python manage.py test的输出被发送到它的stderr 您必须捕获正在生成的进程的 stdout 和 sterr。 可以在此处找到运行命令并捕获两个流的一个很好的示例,总结如下:

import subprocess
import sys



def run(cmd):
    proc = subprocess.Popen(cmd,
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE,
    )
    stdout, stderr = proc.communicate()
 
    return proc.returncode, stdout, stderr
 
code, out, err = run([sys.executable, 'examples/python/run.py'])
 
print("out: '{}'".format(out))
print("err: '{}'".format(err))
print("exit: {}".format(code))

暂无
暂无

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

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