python - 如何在 Django 1.4 网站中显示基本 shell 命令的结果?

标签 python django shell

目前已经使用 Django 构建了静态网页,只是想知道如何在其中一个网页上显示基本 shell 命令(例如“ls -l”)的结果。

可否专门指定某个div来显示结果?

最佳答案

使用 os执行命令的模块:

import os
from django.shortcuts import render

def command_view(request):
    output = os.popen('ls -l').read()
    return render(request, 'command.html', {'output': output})

并将命令的结果放入<pre>标签:

<pre>{{ output }}</pre>

关于python - 如何在 Django 1.4 网站中显示基本 shell 命令的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28027566/

相关文章:

python - 在单个方法上启用 django 的 TEMPLATE_STRING_IF_INVALID

python - 为什么在命令行中使用 sys.argv 时没有引号?

linux - 为什么 SunOS 说它不能执行这个 KornShell 脚本?

python - scikit - 随机森林回归 - AttributeError : 'Thread' object has no attribute '_children'

django - 在Django/Apache中的性能不佳

python - 名称错误 : global name 'NoneType' is not defined in Spark

html - Django/bootstrap 表单 : how to set and get btn-group value

python - 如何编写 python 代码来访问用 C 编写的程序的输入和输出?

python - 在 Python 脚本中保护密码

python - 使用 kivy 和 python 返回屏幕管理器时未设置焦点