urls.py
from django.urls import path
from django.conf.urls import url
from . import views
urlpatterns = [
path('VRFFILELIST/',views.sindex,name="Home-Page"),
]
forms.py:
==========
from django import forms
class stringform(forms.Form):
Enter_VRF_Name_To_Search = forms.CharField(max_length=20)
views.py
===========
from django.shortcuts import render
from .forms import stringform
import os
def sindex(request):
search_path = "C:/AUTOMATE/STRING/TESTFILE/"
file_type = ".txt"
Sstringform = stringform()
Scontext = {'Sstringform' : Sstringform}
if request.method == 'POST':
SVRFTEXT = Scontext['Enter_VRF_Name_To_Search'] = request.POST['Enter_VRF_Name_To_Search']
#fname1 = []
ftext = []
Scontext['resultlist'] = ftext
for fname in os.listdir(path=search_path):
fo = open(search_path + fname)
fread = fo.readline()
line_no = 1
while fread != '':
fread = fo.readline()
if SVRFTEXT in fread:
ftext.append(fread)
line_no +=1
fo.close()
return render(request, 'demoapp/VRFFILELIST.html', Scontext)
VRFFILELIST.HTML
===================
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VRF FILE LIST</title>
</head>
<body>
<form method="POST">
{% csrf_token %}
<table>
{{Sstringform.as_table}}
</table>
<button type="submit">Send Command</button>
</form>
<table>
<h3>VRF IN LIST OF FILES</h3>
<textarea rows="20" cols="80" name="conf" form="conf" id="conf">{{resultlist}}</textarea>
</table>
</body>
</body>
</html>
输出:
[' ip vrf forwarding TEST:VRFA\n', ' ip vrf forwarding TEST:VRFA\n', ' address-family ipv4 vrf TEST:VRFA\n', ' neighbor 10.10.10.1 route-map TEST:VRFA-IN in\n', ' neighbor 10.10.10.1 route-map TEST:VRFA-OUT out\n', ' ip vrf forwarding TEST:VRFA\n', ' neighbor 192.168.10.2 route-map TEST:VRFA-IN in\n', ' neighbor 192.168.10.2 route-map TEST:VRFA-OUT out\n']
所需输出:
TEST-FILE-A:
ip vrf转发测试:VRFA
ip vrf转发测试:VRFA
地址系列ipv4 vrf测试:VRFA
邻居10.10.10.1路由映射测试:VRFA- in in
邻居10.10.10.1路由映射测试:VRFA- out
TEST-FILE-C:
ip vrf转发测试:VRFA
邻居192.168.10.2路由映射测试:VRFA- in in
邻居192.168.10.2路由映射测试:VRFA- out
发布于 2020-06-08 15:09:43
尝试此<textarea rows="20" cols="80" name="conf" form="conf" id="conf">{% for result in resultlist%}{{result}}{% endfor %}</textarea>
https://stackoverflow.com/questions/62256578
复制相似问题