如何在 Django 中将控制台输出显示为 HTML?

आनं*_*नंद 6 html python django

所以我使用 Django 框架将控制台输出显示为 HTML。为了执行命令,我在 Python 中使用check_outputofsubprocess模块。它接收来自 HTML 输入表单的输入。问题是,我只看到“ None” HTML页面,这是默认值上outputviews文件。下面是视图文件和 HTML 文件的代码。我是这方面的新手,因此非常感谢您的帮助。

视图.py

from django.shortcuts import render
from django.shortcuts import redirect
from .forms import command_form
import subprocess as sp


# Create your views here.

def welcome_page(request):
    output=""
    if request.method == "POST":
        myform = command_form(request.POST)
        if (myform.is_valid()):
            execute_command = myform.cleaned_data['cmd_string']
            output = sp.check_output(execute_command, shell=True)
        else:
            myform = command_form()
        return render(request, 'ovs/welcome.html', {'output': output})
    else:
        return render(request, 'ovs/welcome.html', {})
Run Code Online (Sandbox Code Playgroud)

欢迎.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>welcome to ovs GUI</title>
</head>
<body>
    <h3>Choose the option:</h3>
    <form method="POST">{% csrf_token %}
        Enter the command: <input type="text" name="cmd_string" id="cmd_string"/>
        <input type="submit" value="Run"/>
    </form>
    <h3>{{ output }}</h3>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

形式

from django import forms


class command_form(forms.Form):
    command = forms.CharField(max_length=200)
Run Code Online (Sandbox Code Playgroud)

nik*_*k_m 4

您没有将表单字段正确呈现为 HTML。你已经创造了一种command_form形式,但你从不利用它。但是,您应该为 python es 使用驼峰式大小写名称class,如下所示CommandForm

在 HTML 中写入以下内容:

<form method="POST">{% csrf_token %}
    Enter the command: {{ myform }}
    <input type="submit" name="submit_cmd" value="Run" />
</form>
{% if output %}<h3>{{ output }}</h3>{% endif %}
{% if exit_code %}<h3>Your command returned an error: {{ error_msg }}</h3>{% endif %}
Run Code Online (Sandbox Code Playgroud)

{{ my_form }}将自动扩展为<input type="text" ...>

welcome_page现在,像这样写下你的观点:

def welcome_page(request):
    output = ""
    # Initialize the form. At this point you have an unbound/invalid form
    myform = command_form()  # better write it as CommandForm

    if request.method == "POST":
        myform = command_form(request.POST)
        if myform.is_valid():
            # execute_command variable, should now contain the command typed by the user in the text box
            execute_command = myform.cleaned_data['command']
            try:
                # If the return code is non-zero, CalledProcessError will be raised
                output = sp.check_output(execute_command, shell=True)
            except sp.CalledProcessError:
                exit_code, error_msg = output.returncode, output.output
        else:
            # Do something when the form is not valid. Maybe add a message or something, or not implement else clause at all.
    return render(request, 'ovs/welcome.html', locals())
Run Code Online (Sandbox Code Playgroud)

警告!根据文档说:

使用shell=True可能存在安全隐患。