如何将错误异常消息传递到网页上。我正在使用 Atom 文本编辑器和 Django
View .py
try:
netconnect = ConnectHandler(**devices)
except (AuthenticationException):
re = print ('Authentication failed ' + ipInsert)
return render(request,'first_app/forms.html', {'form': form, 'reprinting':re})
表单.html
{% if request.POST %}
<pre>{{ reprinting }}</pre>
{% endif %}
它打印 None 而不是在代码中打印错误消息。
注意:-尽管所描述的错误消息是在文本编辑器的命令行上打印的
完整代码请引用链接: full code
最佳答案
print(..)
[Python-doc]函数不返回内容。它将值打印到标准输出 channel ,并返回 None
。
如果你想让re
包含错误信息,你需要给它赋值,比如:
try:
netconnect = ConnectHandler(**devices)
except AuthenticationException:
<b>re = 'Authentication failed {}'.format(ipInsert)</b>
print(re)
return render(request,'first_app/forms.html', {'form': form, 'reprinting':re})
请注意,您应该检查:
{% if <b>request.method == 'POST'</b> %}
{% endif %}
既然一个POST请求可以为空,那么它仍然是一个POST请求。
关于python - 使用django在网页上传递错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57037072/