简体   繁体   中英

Django View and Python Script Output Display

UPDATE I can now click my button and get the ping output on the console. I edited my "view_a4_gw1.py" and created template "ping_results.html" to display the python script (ping results) output in the Django website. But it returns "None" on the website. Not sure why?

Overview and Objective I have a Python script that SSH to a switch and executes a ping to the neighbor gateway. I'm trying to setup a webpage where I can click on a button and execute the python script and have the ping results display on the web page (or pop-up window/box?).

*The python script works when executed in the console / from the command line. *My Django views, urls, and templates are configured.

Current state When I click the button on the webpage to execute the python script for the ping, I get no output returned. I do notice this message on the web sever console: "GET / HTTP/1.1" 200 981, which looks like the GET was successful.

Code Attached: views.py - My primary view for my template

view_a4_gw1.py - my view for the python script (script is a4_gw1.py) which I import into the view

urls.py - my url.py for my app

ete_dash.html - my template

Ask I just need a hint to steer me in the right direction to display the output from my script. I've read through most of the Django and Python script advice on this site and on the Internet, but so far nothing seems to work.

views.py

from django.shortcuts import render

def ete_dash(request):
    return render(request, 'ete/ete_dash.html', {})

view_a4_gw1.py

 from django.shortcuts import render
 import a4_gw1

 def ping(request):
    output=a4_gw1.a4_gw1()
    return render(request, ‘ete/ping_results.html’, {‘output’:output})

urls.py

    from django.conf.urls import url
    from . import views
    import view_a4_gw1

    urlpatterns = [
        url(r'^$', views.ete_dash, name='ete_dash'),
        url(r’^ping/$', view_a4_gw1.ping, name='ping'),
]

ete_dash.html

{% load staticfiles %}
<html>
   <body> 
    <div class=”button”>
    <a href=”{% url “ping” %}”>sjc12-a4-gw1</a>
    </div>
    </body>
</html>

ping_results.html

<html>
    <head>
        <title>Ping Results</title>
    </head>

    <body>
    <div>
    <pre>{{ output }}</pre>
    </div>
    </body>
</html>

Are you getting any other errors. Looks like you did not import HttpResponse in view_a4_gw1.py

You have two views with the same URL, Django will only use the first one that matches. You need to give ping a different URL; why not r'^ping/$' ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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