Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:执行函数并在模板中打印结果,无需刷新_Python_Html_Django - Fatal编程技术网

Python Django:执行函数并在模板中打印结果,无需刷新

Python Django:执行函数并在模板中打印结果,无需刷新,python,html,django,Python,Html,Django,我正在使用django开发我的网站。在这个页面中,我有x作为输入,当我按下enter键时,将执行一个函数,然后页面将刷新并显示结果。 我想让它动态:捕捉输入,执行函数,显示结果,然后另一个标签出现等等。。 我会非常感激的 这是我的python代码: def console (request): import paramiko import time import getpass import re ip = '192.168.43.10' us

我正在使用django开发我的网站。在这个页面中,我有x作为输入,当我按下enter键时,将执行一个函数,然后页面将刷新并显示结果。 我想让它动态:捕捉输入,执行函数,显示结果,然后另一个标签出现等等。。 我会非常感激的

这是我的python代码:

def console (request):
    import paramiko
    import time
    import getpass
    import re

    ip = '192.168.43.10'

    username = 'osboxes'
    password = 'osboxes.org'

    ssh = paramiko.SSHClient()

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip, username=username, password=password,
                look_for_keys=False, allow_agent=False)
    print('Successfully connected to %s' % ip)
    remote_conn = ssh.invoke_shell()

    time.sleep(.005)
    output = remote_conn.recv(65535)
    print (output)
    def escape_ansi(line):
        ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
        return ansi_escape.sub('', str(line))
    x=request.GET['x']
    time.sleep(1)
    remote_conn.send(x+'\n')
    time.sleep(0.1)
    if remote_conn.recv_ready():
        output = remote_conn.recv(5000)
        op=output.decode()
        oo=escape_ansi(op)

    return render(request,'shell.html', {'oo' : oo})
这是我的html文件:

{% extends 'base.html' %}
{% block content %}
    <div class="mb-3 card text-white card-body" style="background-color: rgb(51, 51, 51); border-color: rgb(51, 51, 51);">
        <h5 class="text-white card-title">Console log to the gateway</h5>
            <form action="console">  
                <div class="position-relative form-group" >
                        <input rows="15" style="color: #FFFFFF; background-color: rgb(51, 51, 51)" name="x" id="x" class="form-control"> 
                </div>
            </form>
    </div>
    <h3>result : {{oo}}</h3>
{% endblock %}
{%extends'base.html%}
{%block content%}
控制台日志到网关
结果:{{oo}
{%endblock%}

您必须理解“呈现”的概念,当服务器完成某项工作,然后返回结果以呈现HTML时。此时,服务器与页面没有任何连接。 这个过程是单向的


我认为你在寻找类似的东西。但是Django没有类似的东西

您必须理解“呈现”的概念,当服务器完成某项工作,然后返回结果以呈现HTML时。此时,服务器与页面没有任何连接。 这个过程是单向的


我认为你在寻找类似的东西。但是Django没有类似的东西

您需要在代码中添加ajax功能。最好从jquery开始,除非使用某种形式的javascript,否则不刷新就无法更新页面。您需要在代码中添加ajax功能。最好从jquery开始,除非使用某种形式的javascript,否则不刷新就无法更新页面。