【发布时间】:2020-09-18 10:29:50
【问题描述】:
我想知道任何人都可以帮助我解决我无法解决的问题,并且真的很困扰,因为我花了最后两个时间但无法做到。
基本上,我正在构建一个应用程序(Django Python)来恢复有关所有网络设备的信息,例如主机名、IP 地址、S/N、机架信息等,但我也启用了一些选项,如添加、编辑、删除和设备输入旁边的连接。我能够创建除 Connect 选项之外的所有选项,我完全卡住了,我正在尝试查询数据库以获取 IP 地址信息,然后使用 Popen 模块打开一个 putty 窗口,其中包含与该 IP 地址设备相关的 ssh,我尽我所能,但没有任何效果,因此,如果您对此有任何想法,请寻求您的帮助?或用户单击连接时的任何其他替代方法腻子或类似应用程序将打开,他只需输入登录凭据并进入设备。
我在这里分享我的代码,如果我做错了什么,请告诉我。
在显示所有设备页面上,我有这个代码,show.html
<td><a href="/connect/{{ i.ip_address }}">Connect</a></td>
<!--<td><a href="/connect/{{ i.id }}">Connect</a></td>-->
我尝试了两种方式,在数据库中输入了id和ip地址
在 view.py 上
def connect(request, ip_address):
hostlist_ip = HostList.ip_address
print(hostlist_ip)
Popen("putty.exe" + hostlist_ip)
return redirect('/show')
在 url.py 中
path('connect/<str:ip_address>', views.connect),
or
path('connect/<str:ip_address>', views.connect),
由于我还在终端上打印输出,我注意到它没有返回实际的 IP 地址,而是返回了这个;
<django.db.models.query_utils.DeferredAttribute object at 0x04B77C50>
在网上我收到了这个错误
TypeError at /connect/10.10.32.10
can only concatenate str (not "DeferredAttribute") to str
Request Method: GET
Request URL: http://localhost:8000/connect/10.10.32.10
Django Version: 2.2.3
Exception Type: TypeError
Exception Value:
can only concatenate str (not "DeferredAttribute") to str
如果您能提供帮助,请告诉我。
仅供参考,我已经通过 python 测试了 Popen,但是由于我们没有从数据库中获取实际的 IP 地址,所以我看到了这个,而且我是一个完整的 html/css 和 Djano 新手,但是我对 python 有一些基本知识,所以请忽略我在帖子中的任何愚蠢的cmets。
非常感谢
【问题讨论】:
-
什么是
i?您能否展示您传递给模板的内容,以及您如何在模板中处理这些内容? -
如果 HostList 是一个模型,那么你正在调用一个字段声明。无论哪种方式,如果不知道你的变量是什么,这很难判断。快速修复将调用 str() ,例如:
Popen("putty.exe" + str(hostlist_ip)). -
嘿@WillemVanOnsem,我只是在show.html中打印表单的for循环之一
-
{% for i in hostlist_key%} <tr> <!--{{ i.id }}</td>--> <td>{{ i.hostname }}</td> <td>{{ i.ip_add }}</td> <td>{{ i.model_number }}</td> <td>{{ i.sw_type }}</td> <td>{{ i.serial }}</td> <td>{{ i.rack }}</td> <td><a href="/edit/{{ i.id }}">Edit</a></td> <td><a href="/delete/{{ i.id }}">Delete</a></td> <td><a href="/connect/{{ i.ip_add }}">Connect</a></td> -
@Melvyn,我尝试在 url.py 中使用 str 并将变量从 ip_address 更改为 ip_add ,因为它似乎已经在 ipaddress 模块下的 python 中,现在我收到此错误@ 987654329@ 和我在 veiw 中的代码看起来像这样
def connect(request, ip_add): ip = HostList.objects.filter(id=ip_add) i = ipaddress.ip_address(ip_add) print(i) return redirect('/show')
标签: django python-3.x python-2.7 django-models