Django show a List in a Template

0

I'm creating a program that pings a host, in a list I'm storing the results of the ping and I want to publish the result in my template but it does not show me anything and I do not know what I'm doing wrong, I hope you can help me

the code is as follows

views.py

def ping(request):
    errors = []
    line = []
    if 'ip1' in request.GET:
        ip1=request.GET['ip1']

        if not ip1:
            if 'ip2' in request.GET:
                ip2=request.GET['ip2']
                if not ip2:
                    errors.append('Por favor introduce una IP.')
                    return render(request, 'ping.html', {'errors':errors})
                else:
                    tre=tracert(ip2)
                    res1= "%s Host Traces" % ip2
                    return render(request, 'ping.html', {'res1':res1})  
            else:
                return render(request, 'ping.html', {'errors':errors})  
        else:
                response = os.system("ping -c 1 " + ip1 + " > /dev/null 2>&1")
                if response == 0:
                    res= "%s Esta En Linea" % ip1
                    os.system("gnome-terminal -e 'bash -c \"ping -c 4 " +ip1+"; exec bash\"'")
                    pin=os.popen('ping -c 4 '+ip1,"r")
                    while True:
                        lines=pin.readline()
                        if not lines: 
                            break
                        line.append(lines)

                    return render(request, 'ping.html', {'res':res}, {'line':line}) 
                else:
                    res= "%s No Esta En Linea" % ip1
                    return render(request, 'ping.html', {'res':res})
    else:
        return render(request, 'ping.html', {'errors':errors})

def tracert(ip2):
    ip2t=ip2
    os.system("gnome-terminal -e 'bash -c \"traceroute " +ip2t+"; exec bash\"'")

in the lisa line I store the results when I print the list it shows me that it was stored correctly and then the return but at the moment of publishing in the Template it does not show me anything attached to the Template

<!DOCTYPE>
<html>
<head>
    {% load static from staticfiles %} 
    <link rel="stylesheet" href= "{% static "css/ping/css/style.css" %}">
    <link rel="stylesheet" href= "{% static "css/ping/css/style.css" %}" type="text/css" media="all">
    <meta charset="utf-8">
    <meta name="autor"  content="Mikey">
    <meta name="description" content="SYSTEM HACKING">
    <title>PING SYSTEM</title>
</head>
<body>
    <form action="{%  url 'ping' %}" method="get">{% csrf_token %}
        {% if errors %} 
            <ul> 
                {% for error in errors %} 
                <li style="color: red;">{{ error }}</li> 
                {% endfor %} 
            </ul> 
        {% endif %} 
        <h1><a target="_blank">HACKING SYSTEM PING</a></h1>

        <table class="table table-hover" align="center" border="1">
            <thead align="center">
                <tr align="center">
                    <th></th>
                    <th align="center"><p style="color:white;">PING</p></th>
                    <th></th>
                    <th></th>
                    <th align="center"><p style="color:white;">TRACER ROUTE</p></th>
                    <th></th>
                </tr> 
            </thead>
            <tbody>
                <tr align="center">
                    <td align="center" style="color: white">IP HOST</td>
                    <td align="center"> <input type="text" name="ip1" align="center"> </td>
                    <td align="center">  <input type="submit" name="boton" value="PING" align="center">  </td>
                    <td align="center" style="color: white">IP HOST</td>
                    <td align="center"> <input type="text" name="ip2" align="center"> </td>
                    <td align="center">  <input type="submit" name="boton" value="TRACER" align="center">  </td>
                </tr>
            </tbody> 
        </table>

        <table align="center">
            <thead>
                <tr>
                    <th align="center" style="color: white"></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td align="center">
                        {% if line%}
                            {% for i in line %}
                                <TEXTAREA rows="5" cols="30" name="txtsugerencias">{{i}}</TEXTAREA><BR>
                            {% endfor %}
                        {% endif %}
                    </td>
                </tr>
            </tbody>
        </table>

        {% if res %}
            <h2 align="center"><p style="color:white;">Resultados PING {{ res }} </p></h2> 
        {% endif %}

         {% if res1 %}
            <h2 align="center"><p style="color:white;">Tracert En Curso {{ res1 }}</p></h2> 
        {% endif %}

        <table align="center">

                <tr align="center">
                    <td>
                        <a href="/menu" style="color: white"> Exit </a>
                    </td>
                </tr>   

        </table>    


    </form>
</body>
</html>

This is my HTML code I want to show the data in a TEXTAREA but it does not show me anything I hope you can help me since I tried in many ways and I have not managed to show this information

Summary

In case my explanation was not clear, with pin=os.popen('ping -c 4 '+ip1,"r") , I ping a host that the user enters, I cycle to store the results in a list

while True:
    lines=pin.readline()
    if not lines: 
       break
    line.append(lines)

Now I return the line list to the return render(request, 'ping.html', {'res':res}, {'line':line}) template

But when I show the line list in the template it does not show me anything the code I'm using is

{% if line%}
                        {% for i in line %}
                            <TEXTAREA rows="5" cols="30" name="txtsugerencias">{{i}}</TEXTAREA><BR>
                        {% endfor %}
                    {% endif %}
    
asked by mikey 11.09.2018 в 07:25
source

1 answer

0

Hi, everyone, I want to give thanks for the help I got to fix the error, the solution works two steps first.

the code of views.py

def ping(request):
errors = []
line = []
if 'ip1' in request.GET:
    ip1=request.GET['ip1']

    if not ip1:
        if 'ip2' in request.GET:
            ip2=request.GET['ip2']
            if not ip2:
                errors.append('Por favor introduce una IP.')
                return render(request, 'ping.html', {'errors':errors})
            else:
                tre=tracert(ip2)
                res1= "%s Host Traces" % ip2
                return render(request, 'ping.html', {'res1':res1})  
        else:
            return render(request, 'ping.html', {'errors':errors})  
    else:
            response = os.system("ping -c 1 " + ip1 + " > /dev/null 2>&1")
            if response == 0:
                res= "%s Esta En Linea" % ip1
                line=pingaux(ip1)
                os.system("gnome-terminal -e 'bash -c \"ping -c 4 " +ip1+"; exec bash\"'")
                #context={'line':line}
                return render(request, 'ping.html',{'line':line}, {'res':res})  
            else:
                res= "%s No Esta En Linea" % ip1
                return render(request, 'ping.html', {'res':res})
else:
    return render(request, 'ping.html', {'errors':errors})


def tracert(ip2):
ip2t=ip2
os.system("gnome-terminal -e 'bash -c \"traceroute " +ip2t+"; exec bash\"'")

def pingaux(ip1):
line1=[]
ip1t=ip1
pin=os.popen('ping -c 4 '+ip1t,"r")
while True:
    lines=pin.readline()
    if not lines:break
    line1.append(lines)
return line1

Good, change the code and create a function to store the ping in the list and return the value

in the html code

 {% if line %}
                            <textarea rows="12" cols="51" style="color: red; background-color: black" >{{ line }}</textarea>
                            <h2 align="center"><p style="color: white"></p></h2>
                    {% endif %}

in this way the whole arrangement is shown within the textarea and so it publishes the whole arrangement

and finally although it sounds crazy or strange restart my server where I work and develop my applications that debian 9 and restart the django server and so it worked, and after testing the restart of the server was the real solution

    
answered by 12.09.2018 в 04:58