用djang在网页上传递错误信息

2024-06-16 16:11:14 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将错误异常消息传递到网页上。我正在使用Atom文本编辑器和django

你知道吗视图.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 %}

它的pritingNone而不是用代码打印错误消息。你知道吗

你知道吗注:尽管描述的错误消息正在文本编辑器的命令行上打印

有关完整代码,请参阅以下链接: full code


Tags: django代码pyreform视图消息网页
2条回答

^{} [Python-doc]函数不返回内容。它将值打印到标准输出通道,并返回None。你知道吗

如果希望re包含错误消息,则需要分配它,如:

try:
    netconnect = ConnectHandler(**devices)
except AuthenticationException:
    re = 'Authentication failed {}'.format(ipInsert)
    print(re)
    return render(request,'first_app/forms.html', {'form': form, 'reprinting':re})

请注意,您应该检查:

{% if request.method == 'POST' %}
{% endif %}

因为POST请求可以为空,但它仍然是POST请求。你知道吗

        try:
            netconnect = ConnectHandler(**devices)
        except (AuthenticationException):
            re = ('Authentication failed ' + str(ipInsert))
            return render(request,'first_app/forms.html', {'form': form, 'reprinting':re})

这应该够了

相关问题 更多 >