我想在 Django 中使用 .py 文件,但遇到了一些问题我是全新的,需要一些解决方案
I want to use the .py file in Django but faced some issues I'm totally new and need some solution
我制作了一个 python 脚本来查找给定数量的主机的子网掩码。但我希望它在单击按钮时接受 HTML 输入,并将其作为用户输入传递到 python 脚本中,并在同一 HTML 页面上提供输出。
我已经尝试向你们提供该程序的最大详细信息
我无法理解这个问题。它显示在 url 中输入,就像这里一样,终端输出用于输入 500
urls.py
path('findSM_H', views.findSM_H, name='findSM_H'),
path('findSM_Houtput', views.findSM_Hexecution, name='findSM_Houtput')
findSM_H.html
<form method="GET" action="/findSM_H">
{% csrf_token %}
<table>
<tbody>
<tr>
<td width="450">
<table width="450">
<tbody>
<tr>
\TAKING INPUT FROM THE USER
<td width="250" height="50">
<label for="noofhostinput"> Number of host </label>
</td>
<td width="250" height="50">
//ON CLICKING THE BUTTON IT TAKES TO 'findSM_Houtput' FUNCTION
<input id="noofhostinput" type="text" name="noofhostinput" />
<input type="submit" onclick="location.href ='{%url 'findSM_Houtput'%}'" >
</td>
</tr>
</tbody>
</table>
</td>
<td width="30"></td>
<td width="450">
<table width="450">
<tbody>
<tr>
<td width="250" height="50">
//OUTPUT OF THE USER INPUT
<label for="subnetmask"> Subnet mask </label>
</td>
<td width="200" height="50">
<div id="subnetmask" style="background-color: ddb(39, 6, 39)" type="text">
00{{noofhost_op}}
</div>
<h3>0.{{noofhost_op}}</h3>
</td>
</tr>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>
views.py
def findSM_H(request):
return render(request, 'findSM_H.html')
def findSM_Hexecution(request):
noofhost_input = request.GET.get("noofhostinput")
noofhost_output = run([sys.executable, 'D:\Network_Project\NetworkTools\use_host_to_find_subnet_Mask_with_value.py',
noofhost_input], shell=False, stdout=PIPE)
print(noofhost_output)
return render(request, 'findSM_H.html', {'noofhost_op': noofhost_output})
use_host_to_find_subnet_Mask_with_value.py
import sys
noofhostinput="%s" % (sys.argv[1])
usr = int(noofhostinput)
table = [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,
32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216]
for i in table:
if usr < i:
n = table.index(i)
break
csm = 32- n
if csm >=8 or csm<33:
sm={'8':'255.0.0.0',
'9':'255.128.0.0',
'10':'255.192.0.0',
'11':'255.224.0.0',
'12':'255.240.0.0',
'13':'255.248.0.0',
'14':'255.252.0.0',
'15':'255.254.0.0',
'16':'255.255.0.0',
'17':'255.255.128.0',
'18':'255.255.192.0',
'19':'255.255.224.0',
'20':'255.255.240.0',
'21':'255.255.248.0',
'22':'255.255.252.0',
'23':'255.255.254.0',
'24':'255.255.255.0',
'25':'255.255.255.128',
'26':'255.255.255.192',
'27':'255.255.255.224',
'28':'255.255.255.240',
'29':'255.255.255.248',
'30':'255.255.255.252',
'31':'255.255.255.254',
'32':'255.255.255.255'}
for i in range(8,33):
if i == csm:
j= str(i)
output = f'your subnet mask for {usr} Host is {sm[j]}/{csm}'
print(output)
else:
output="your value is out of range of valid subnet mask."
print(output)
终端日志
[14/Feb/2022 05:45:30] "GET /findSM_H?csrfmiddlewaretoken=4SK8zRYZkwzVYb8RP80A8Wu3tILyhJGejbsAfKTHUAh9VgjGMfDdFjtSVFLexTlD&noofhostinput=500 HTTP/1.1" 200 4868Internal Server Error: /findSM_Houtput
Traceback (most recent call last):
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Network_Project\NetworkTools\NetworkApps\views.py", line 24, in findSM_Hexecution
noofhost_output = run([sys.executable, 'D:\Network_Project\NetworkTools\use_host_to_find_subnet_Mask_with_value.py',
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1360, in _execute_child
args = list2cmdline(args)
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 565, in list2cmdline
for arg in map(os.fsdecode, seq):
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\os.py", line 822, in fsdecode
filename = fspath(filename) # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not NoneType
[14/Feb/2022 05:45:30,806] - Broken pipe from ('127.0.0.1', 49451)
伙计们,这里出了什么问题,django 想说什么?
任何建议都会有所帮助
您可以在从浏览器提交时检查 noofhost_input 是否为空,因为我认为您的表单输入提交操作已被覆盖(见下文):
def findSM_Hexecution(request):
noofhost_input = request.GET.get("noofhostinput")
print("******",noffhost_input,"*****")
noofhost_output = run([sys.executable, 'D:\Network_Project\NetworkTools\use_host_to_find_subnet_Mask_with_value.py',
noofhost_input], shell=False, stdout=PIPE)
print(noofhost_output)
我想 noffhost_input 是空的,因为 onclick 定义覆盖了表单操作但没有提交表单的输入字段值。
<input type="submit" onclick="location.href ='{%url 'findSM_Houtput'%}'"
顺便说一句:表单操作指向另一个地址。
解决方案是删除 onclick... 并将表单操作指向 findSM_Houtput.
我制作了一个 python 脚本来查找给定数量的主机的子网掩码。但我希望它在单击按钮时接受 HTML 输入,并将其作为用户输入传递到 python 脚本中,并在同一 HTML 页面上提供输出。 我已经尝试向你们提供该程序的最大详细信息 我无法理解这个问题。它显示在 url 中输入,就像这里一样,终端输出用于输入 500
urls.py
path('findSM_H', views.findSM_H, name='findSM_H'),
path('findSM_Houtput', views.findSM_Hexecution, name='findSM_Houtput')
findSM_H.html
<form method="GET" action="/findSM_H">
{% csrf_token %}
<table>
<tbody>
<tr>
<td width="450">
<table width="450">
<tbody>
<tr>
\TAKING INPUT FROM THE USER
<td width="250" height="50">
<label for="noofhostinput"> Number of host </label>
</td>
<td width="250" height="50">
//ON CLICKING THE BUTTON IT TAKES TO 'findSM_Houtput' FUNCTION
<input id="noofhostinput" type="text" name="noofhostinput" />
<input type="submit" onclick="location.href ='{%url 'findSM_Houtput'%}'" >
</td>
</tr>
</tbody>
</table>
</td>
<td width="30"></td>
<td width="450">
<table width="450">
<tbody>
<tr>
<td width="250" height="50">
//OUTPUT OF THE USER INPUT
<label for="subnetmask"> Subnet mask </label>
</td>
<td width="200" height="50">
<div id="subnetmask" style="background-color: ddb(39, 6, 39)" type="text">
00{{noofhost_op}}
</div>
<h3>0.{{noofhost_op}}</h3>
</td>
</tr>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>
views.py
def findSM_H(request):
return render(request, 'findSM_H.html')
def findSM_Hexecution(request):
noofhost_input = request.GET.get("noofhostinput")
noofhost_output = run([sys.executable, 'D:\Network_Project\NetworkTools\use_host_to_find_subnet_Mask_with_value.py',
noofhost_input], shell=False, stdout=PIPE)
print(noofhost_output)
return render(request, 'findSM_H.html', {'noofhost_op': noofhost_output})
use_host_to_find_subnet_Mask_with_value.py
import sys
noofhostinput="%s" % (sys.argv[1])
usr = int(noofhostinput)
table = [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,
32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216]
for i in table:
if usr < i:
n = table.index(i)
break
csm = 32- n
if csm >=8 or csm<33:
sm={'8':'255.0.0.0',
'9':'255.128.0.0',
'10':'255.192.0.0',
'11':'255.224.0.0',
'12':'255.240.0.0',
'13':'255.248.0.0',
'14':'255.252.0.0',
'15':'255.254.0.0',
'16':'255.255.0.0',
'17':'255.255.128.0',
'18':'255.255.192.0',
'19':'255.255.224.0',
'20':'255.255.240.0',
'21':'255.255.248.0',
'22':'255.255.252.0',
'23':'255.255.254.0',
'24':'255.255.255.0',
'25':'255.255.255.128',
'26':'255.255.255.192',
'27':'255.255.255.224',
'28':'255.255.255.240',
'29':'255.255.255.248',
'30':'255.255.255.252',
'31':'255.255.255.254',
'32':'255.255.255.255'}
for i in range(8,33):
if i == csm:
j= str(i)
output = f'your subnet mask for {usr} Host is {sm[j]}/{csm}'
print(output)
else:
output="your value is out of range of valid subnet mask."
print(output)
终端日志
[14/Feb/2022 05:45:30] "GET /findSM_H?csrfmiddlewaretoken=4SK8zRYZkwzVYb8RP80A8Wu3tILyhJGejbsAfKTHUAh9VgjGMfDdFjtSVFLexTlD&noofhostinput=500 HTTP/1.1" 200 4868Internal Server Error: /findSM_Houtput
Traceback (most recent call last):
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Network_Project\NetworkTools\NetworkApps\views.py", line 24, in findSM_Hexecution
noofhost_output = run([sys.executable, 'D:\Network_Project\NetworkTools\use_host_to_find_subnet_Mask_with_value.py',
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1360, in _execute_child
args = list2cmdline(args)
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 565, in list2cmdline
for arg in map(os.fsdecode, seq):
File "C:\Users\NIRMAL\AppData\Local\Programs\Python\Python39\lib\os.py", line 822, in fsdecode
filename = fspath(filename) # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not NoneType
[14/Feb/2022 05:45:30,806] - Broken pipe from ('127.0.0.1', 49451)
伙计们,这里出了什么问题,django 想说什么? 任何建议都会有所帮助
您可以在从浏览器提交时检查 noofhost_input 是否为空,因为我认为您的表单输入提交操作已被覆盖(见下文):
def findSM_Hexecution(request):
noofhost_input = request.GET.get("noofhostinput")
print("******",noffhost_input,"*****")
noofhost_output = run([sys.executable, 'D:\Network_Project\NetworkTools\use_host_to_find_subnet_Mask_with_value.py',
noofhost_input], shell=False, stdout=PIPE)
print(noofhost_output)
我想 noffhost_input 是空的,因为 onclick 定义覆盖了表单操作但没有提交表单的输入字段值。
<input type="submit" onclick="location.href ='{%url 'findSM_Houtput'%}'"
顺便说一句:表单操作指向另一个地址。 解决方案是删除 onclick... 并将表单操作指向 findSM_Houtput.